package com.service;

import static com.def.GenericDef.BANK_IN;
import static com.def.GenericDef.EMPTY_CARRIER;
import static com.def.GenericDef.EVT_CHNAGE_LOT;
import static com.def.GenericDef.EVT_JUMP_OPE;
import static com.def.GenericDef.EVT_LGOF;
import static com.def.GenericDef.EVT_REWK_IN;
import static com.def.GenericDef.EVT_WIP_BANK_IN;
import static com.def.GenericDef.EXIST_CARRIER;
import static com.def.GenericDef.E_BIS_DATA;
import static com.def.GenericDef.E_BIS_OPE;
import static com.def.GenericDef.E_BIS_PATH;
import static com.def.GenericDef.E_BIS_PATHI;
import static com.def.GenericDef.E_BIS_PATH_SWH;
import static com.def.GenericDef.E_BIS_TOOL_GR;
import static com.def.GenericDef.E_READ_NOT_FOUND;
import static com.def.GenericDef.E_RET_BOX;
import static com.def.GenericDef.E_RET_PRD_INFO;
import static com.def.GenericDef.E_RET_PRD_OUT;
import static com.def.GenericDef.E_RET_WO;
import static com.def.GenericDef.EVT_SKIP_OPE;
import static com.def.GenericDef.E_XPMOVEIN_BOX_STATUS_INVALIED;
import static com.def.GenericDef.E_XPMOVEOUT_BOX_AND_PRD_COUNT_NOT_MATCH;
import static com.def.GenericDef.E_XPMOVEOUT_INPUT_ITEM_EMPT;
import static com.def.GenericDef.E_XPMOVEOUT_INVALIED_BNK_FLG;
import static com.def.GenericDef.E_XPMOVEOUT_IS_DM_WIP_WAIT;
import static com.def.GenericDef.E_XPMOVEOUT_IS_JB_WIP_WAIT;
import static com.def.GenericDef.E_XPMOVEOUT_JUMP_TO_OPE_NOT_EXIST;
import static com.def.GenericDef.E_XPMOVEOUT_OUT_TOOLG_UNMATCH;
import static com.def.GenericDef.E_XPMOVEOUT_REWK_PARENT_INVALID;
import static com.def.GenericDef.E_XPMOVEOUT_SHT_STATUS_INVALIED;
import static com.def.GenericDef.E_XPMOVEOUT_TOOL_STATUS_INVALIED;
import static com.def.GenericDef.E_XPMOVEOUT_TOOL_UNIT_TYPE_INVALIED;
import static com.def.GenericDef.E_XPMOVEOUT_WO_UNMATCH;
import static com.def.GenericDef.E_XPMOVEOUT_INVALID_PATH_CATE;
import static com.def.GenericDef.E_XPSKIPOPE_INVALIED_NOT_EQUAL;
import static com.def.GenericDef.E_XPSKIPOPE_INVALIED_TWO;
import static com.def.GenericDef.LOT_SPLIT_0;
import static com.def.GenericDef.PATH_TYPE_INVALID;
import static com.def.GenericDef.PATH_TYPE_MAIN;
import static com.def.GenericDef.PATH_TYPE_REWK;
import static com.def.GenericDef.PATH_TYPE_SAMP;
import static com.def.GenericDef.PROC_COMPLETE;
import static com.def.GenericDef.PROC_EMPTY;
import static com.def.GenericDef.PROC_IN_PROCESS;
import static com.def.GenericDef.PROC_WAIT;
import static com.def.GenericDef.RETURN_CODE_OK;
import static com.def.GenericDef.RETURN_CODE_SET_ERROR;
import static com.def.GenericDef.RETURN_CODE_UNKNOWN;
import static com.def.GenericDef.RETURN_MESG_OK;
import static com.def.GenericDef.RUL_ACT_REWORK_OUT;
import static com.def.GenericDef.SLOT_NO_INVALIED;
import static com.def.GenericDef.SWH_TYP_REWORK;
import static com.def.GenericDef.TOOL_STAT_DOWN;
import static com.def.GenericDef.TOOL_STAT_ENG;
import static com.def.GenericDef.TOOL_UNIT_MAIN;
import static com.def.GenericDef.TRX_OUT;
import static com.def.GenericDef.T_XPMOVEOUT;
import static com.def.GenericDef.WIP_REATIN;
import static com.def.GenericDef._NORMAL;
import static com.def.GenericDef._OK;
import static com.def.GenericDef._SPACE;
import static com.def.GenericDef._YES;
import static com.def.GenericDef.T_AMSDMIC;
import static com.def.GenericDef.T_AMSICGOOD;
import static com.def.GenericDef.T_AMSZJ;
import static com.def.GenericDef.T_AMSLOTBROKEN;
import static com.def.GenericDef.TRX_IN;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.hibernate.LockMode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import com.dao.IBisDataDao;
import com.dao.IBisOpeDao;
import com.dao.IBisPathDao;
import com.dao.IBisPathSwhDao;
import com.dao.IBisPathiDao;
import com.dao.IBisToolDao;
import com.dao.IBisToolGrDao;
import com.dao.IRetBoxDao;
import com.dao.IRetCrtNoDao;
import com.dao.IRetPginfoSetDao;
import com.dao.IRetPrdInDao;
import com.dao.IRetPrdInfoDao;
import com.dao.IRetPrdLotDao;
import com.dao.IRetPrdOutDao;
import com.dao.IRetTmCusPrdInDao;
import com.dao.IRetTmCusPrdIndtDao;
import com.dao.IRetToolStDao;
import com.dao.IRetWODao;
import com.icim.service.ICIMBaseService;
import com.model.Bis_bom;
import com.model.Bis_data;
import com.model.Bis_ope;
import com.model.Bis_opeId;
import com.model.Bis_path;
import com.model.Bis_pathId;
import com.model.Bis_path_swh;
import com.model.Bis_path_swhId;
import com.model.Bis_pathi;
import com.model.Bis_pathiId;
import com.model.Bis_tool;
import com.model.Bis_tool_gr;
import com.model.Ret_box;
import com.model.Ret_crt_no;
import com.model.Ret_crt_noId;
import com.model.Ret_pginfo_set;
import com.model.Ret_pginfo_setId;
import com.model.Ret_prd_in;
import com.model.Ret_prd_info;
import com.model.Ret_prd_lot;
import com.model.Ret_prd_lotId;
import com.model.Ret_prd_out;
import com.model.Ret_prd_outId;
import com.model.Ret_tm_cus_prd_in;
import com.model.Ret_tool_st;
import com.model.Ret_wo;
import com.rtnBean.NotFoundRtnBeen;
import com.rtnBean.SimpleRtnBeen;
import com.rtnBean.logoff.NxOpeAll;
import com.tx.Amsbroken.AmsbrokenI;
import com.tx.Amsdmic.AmsdmicI;
import com.tx.Amsicgood.AmsicgoodI;
import com.tx.Amszj.AmszjI;
import com.tx.Xpmoveout.XpmoveoutI;
import com.tx.Xpmoveout.XpmoveoutO;
import com.util.GUIDGenerator;
import com.util.HCutil;
import com.util.MqOpe;
import com.util.Util;

@Scope("prototype")
@Service("XpmoveoutService")
public class XpmoveoutService extends ICIMBaseService{
	private static Logger logger = Logger.getLogger(XpmoveoutService.class);
	private static String TX_NAME = T_XPMOVEOUT;

	public static final String MDL_CATE_JB = "A";
	public static final String MDL_CATE_DM = "B";
	public static final String MDL_CATE_QG = "C";

	private XpmoveoutI inTrx;
	private XpmoveoutO outTrx;
	private MqOpe mqOpe;

	private String gszMsgBuf = "";
	private Timestamp gEvtTimestamp;
	private Double gEvtTimeD;
	private String gEvtSeqID;

	private Bis_tool_gr gBis_tool_gr;
	private Bis_tool gBis_tool;
	//	private Ret_tool_st gRet_tool_st;
	private Ret_box gRet_box;
	private Ret_wo  gRet_wo;

	private Ret_pginfo_set  gRet_pginfo_set;

	private List<Ret_prd_info> gRet_prd_infoList;
	private List<Ret_prd_info> gRet_prd_infoList_movein;

	private Bis_path_swh gBis_path_swh;
	private Bis_pathi gBis_pathi_rwk;
	private Bis_path gBis_path_rwk;
	private Bis_ope gBis_ope_rwk;
	private Bis_data gBis_data_PRST;

	private Integer prdLotCnt = 0;

	private String gProc_id = "";
	private String gCus_id = "";
	private String gWo_id = "";
	private String gMdl_cate = "";
	private String gSo_id = "";
	private String gOpe_dsc = "";
	private String gShip_box_id = "";
	private String gTthickness = "";
	private String gCthickness = "";
	private String gMdl_id = "";
	private String plant_id = "";
	private String cus_id="";

	@Autowired
	private IBisToolDao bisToolDao;
	@Autowired
	private IBisToolGrDao bisToolGrDao;
	//	@Autowired
//	private IRetToolStDao retToolStDao;
	@Autowired
	private IRetBoxDao retBoxDao;
	@Autowired
	private IRetPrdInfoDao retPrdInfoDao;
	@Autowired
	private IBisOpeDao bisOpeDao;
	@Autowired
	private IBisPathDao bisPathDao;
	@Autowired
	private IBisPathiDao bisPathiDao;
	@Autowired
	private IRetPrdOutDao retPrdOutDao;
	@Autowired
	private IRetPrdInDao retPrdInDao;

	@Autowired
	private IRetWODao retWoDao;

	@Autowired
	private IBisPathSwhDao bisPathSwhDao;
	@Autowired
	private IRetPrdLotDao retPrdLotDao;
	@Autowired
	private IBisDataDao bisDataDao;
	@Autowired
	private IRetWODao retWODao;
	@Autowired
	private IRetPginfoSetDao retPginfoSetDao;
	@Autowired
	private IRetCrtNoDao retCrtNoDao;
	@Resource
	private IRetTmCusPrdInDao retTmCusPrdInDao;
	@Autowired
	private SendMsgService sendMsgService;

	public String subMainProc(String strInMsg) {
		long rtnCode = _NORMAL;

		String outMsg = null;
		gszMsgBuf = "";
		gEvtTimestamp = Util.getCurrentTimestamp();
		gEvtTimeD = Util.getDoubleTime();
		gEvtSeqID = GUIDGenerator.javaGUID();
		inTrx = new XpmoveoutI();
		outTrx = new XpmoveoutO();
		outTrx.setTrx_id(TX_NAME);
		outTrx.setType_id(TRX_OUT);
		outTrx.setRtn_code(String.valueOf(RETURN_CODE_SET_ERROR));
		mqOpe = new MqOpe();

		try {
			inTrx = (XpmoveoutI) mqOpe.inStr2Obj(strInMsg, inTrx);
			logger.info("InTrx is " + strInMsg);

			rtnCode = subMainProc2();
			if (rtnCode != _NORMAL) {
				TransactionAspectSupport.currentTransactionStatus()
						.setRollbackOnly();
				outTrx.setRtn_code(String.valueOf(rtnCode));
				outTrx.setRtn_mesg(gszMsgBuf);
			} else {
				outTrx.setRtn_code(String.valueOf(RETURN_CODE_OK));
				outTrx.setRtn_mesg(RETURN_MESG_OK);
				retPrdInfoDao.flushCurSession();
			}
		} catch (Exception ex) {
			TransactionAspectSupport.currentTransactionStatus()
					.setRollbackOnly();
			outTrx.setRtn_mesg(ex.toString());
			outTrx.setRtn_code(RETURN_CODE_UNKNOWN);
			logger.error(Util.stackTraceToString(ex));
		} finally {
			outMsg = mqOpe.obj2Str(outTrx);
			return outMsg;
		}
	}

	/**
	 * New all gloabl values
	 */
	private void iniGloablValues() {
		gBis_tool_gr = new Bis_tool_gr();
		gBis_tool = new Bis_tool();
//		gRet_tool_st = new Ret_tool_st();
		gRet_box = new Ret_box();
		gRet_prd_infoList = new ArrayList<Ret_prd_info>();
		gRet_prd_infoList_movein = new ArrayList<Ret_prd_info>();

		gBis_path_swh = new Bis_path_swh();
		gBis_pathi_rwk = new Bis_pathi();
		gBis_path_rwk = new Bis_path();
		gBis_ope_rwk = new Bis_ope();
		gBis_data_PRST = new Bis_data();
	}

	/**
	 * All move in logic
	 *
	 * @return
	 */
	private long subMainProc2() {
		long rtnCode = _NORMAL;

		// New all gloabl values
		iniGloablValues();

		// Check inTrx items
		rtnCode = checkInTtx();
		if (rtnCode != _NORMAL) {
			return rtnCode;
		}
//        //check 007 for 1100
//		if("C".equals(inTrx.getGchk_flg())){
//		    rtnCode = check007Info();
//		    if (rtnCode != _NORMAL) {
//			  return rtnCode;
//		    }else{
//		      return 123;
//		    }
//		}
		// Get tool info
		rtnCode = getToolInfo();
		if (rtnCode != _NORMAL) {
			return rtnCode;
		}

		// Get box info
		rtnCode = getRetBoxInfo();
		if (rtnCode != _NORMAL) {
			return rtnCode;
		}
		// Check Lot&Box wo
		rtnCode = checkLotInfo();
		if (rtnCode != _NORMAL) {
			return rtnCode;
		}
		// Get sht list info
		rtnCode = getPrdListInfo();
		if (rtnCode != _NORMAL) {
			return rtnCode;
		}


		// Check sht list(status & tool)
		rtnCode = chkShtList();
		if (rtnCode != _NORMAL) {
			return rtnCode;
		}

		rtnCode = checkRwkRoute();
		if (rtnCode != _NORMAL) {
			return rtnCode;
		}

		// Get [DATA-PRST] by proc_id
		rtnCode = getPRST();
		if (rtnCode != _NORMAL) {
			return rtnCode;
		}


		// Set sht pgin
		rtnCode =setPgInfo();
		if (rtnCode != _NORMAL) {
			return rtnCode;
		}

		// Check slot no
		rtnCode = chkSlotNo();
		if (rtnCode != _NORMAL) {
			return rtnCode;
		}

//		rtnCode = checkWipWaitBox();
//		if (rtnCode != _NORMAL) {
//			outTrx.setWip_wait_flg("true");
//			return _NORMAL;
//		}
		rtnCode = checkWipWaitUas();
		if (rtnCode != _NORMAL) {
			return rtnCode;
		}
		// Mtrl box check
		rtnCode = mtrlBoxCheck();
		if (rtnCode != _NORMAL) {
			return rtnCode;
		}

		//华星包装出账出原箱
		String mtrlBoxIdFk = gRet_prd_infoList.get(0).getMtrl_box_id_fk();
//		if("DMBZ".equals(gProc_id) && "007".equals(gCus_id)){
//			if(!inTrx.getBox_id().equals(mtrlBoxIdFk)){
//				gszMsgBuf = "华星包装出账应为原料箱号，请重新确认！";
//				return Integer.valueOf(RETURN_CODE_UNKNOWN);
//			}
//		}

		// Jump ope
		if (!StringUtils.isEmpty(inTrx.getJump_ope_id())
				&& !StringUtils.isEmpty(inTrx.getJump_ope_ver())) {
			rtnCode = jumpOpeProc();
			if (rtnCode != _NORMAL) {
				return rtnCode;
			}
		} else {
			gRet_box.setEvt_cate(EVT_LGOF);
			gRet_box.setEvt_time_d(gEvtTimeD);
			gRet_box.setEvt_timestamp(gEvtTimestamp);
			gRet_box.setEvt_usr(inTrx.getEvt_usr());
			retBoxDao.update(gRet_box);
		}

		if (!StringUtils.isEmpty(inTrx.getSwh_path_id())) {
			Bis_pathiId bis_pathiId_rwk = new Bis_pathiId();
			Bis_pathId bis_pathId_rwk = new Bis_pathId();
			Bis_opeId bis_opeId_rwk = new Bis_opeId();

			// 查询重工途程基本信息
			bis_pathiId_rwk.setPath_id(inTrx.getSwh_path_id());
			bis_pathiId_rwk.setPath_ver(inTrx.getSwh_path_ver());
			gBis_pathi_rwk = bisPathiDao.get(bis_pathiId_rwk);
			if (null == gBis_pathi_rwk) {
				gszMsgBuf = new NotFoundRtnBeen(bis_pathiId_rwk.toString(),
						Bis_pathi.class).toString();
				return E_BIS_PATHI + E_READ_NOT_FOUND;
			}

			// 查询重工途程首站点
			bis_pathId_rwk.setPath_id_fk(gBis_pathi_rwk.getId().getPath_id());
			bis_pathId_rwk.setPath_ver_fk(gBis_pathi_rwk.getId().getPath_ver());
			bis_pathId_rwk.setCr_ope_no(gBis_pathi_rwk.getFirst_ope_no());
			gBis_path_rwk = bisPathDao.get(bis_pathId_rwk);
			if (null == gBis_path_rwk) {
				gszMsgBuf = new NotFoundRtnBeen(bis_pathId_rwk.toString(),
						Bis_path.class).toString();
				return E_BIS_PATH + E_READ_NOT_FOUND;
			}

			// 查询站点信息 gBis_ope_rwk
			bis_opeId_rwk.setOpe_id(gBis_path_rwk.getCr_ope_id_fk());
			bis_opeId_rwk.setOpe_ver(gBis_path_rwk.getCr_ope_ver_fk());
			gBis_ope_rwk = bisOpeDao.get(bis_opeId_rwk);
			if (null == gBis_ope_rwk) {
				gszMsgBuf = new NotFoundRtnBeen(bis_opeId_rwk.toString(),
						Bis_ope.class).toString();
				return E_BIS_OPE + E_READ_NOT_FOUND;
			}
		}

		gShip_box_id = gRet_box.getShip_box_id();
		rtnCode = crtShipBoxId();
		if (rtnCode != _NORMAL) {
			return rtnCode;
		}


		// Move out
		rtnCode = moveOutProc();
		if (rtnCode != _NORMAL) {
			return rtnCode;
		}

		// Change lot id for JBQA
		rtnCode = lotChangeProc();
		if (rtnCode != _NORMAL) {
			return rtnCode;
		}

		// Reset box info
		rtnCode = configureBox();
		if (rtnCode != _NORMAL) {
			return rtnCode;
		}

		// Add lot info, just UPK use
		rtnCode = addLotInfo();
		if (rtnCode != _NORMAL) {
			return rtnCode;
		}

		// Rework from 2200 auto
		if (!StringUtils.isEmpty(inTrx.getRwk_id())) {
			rtnCode = reworkProc();
			if (rtnCode != _NORMAL) {
				return rtnCode;
			}
		}

		//QA move === jump
		/*rtnCode = skipQaFunc();
		if (rtnCode != _NORMAL) {
			return rtnCode;
		}*/

		// Update wo count
		if (PROC_COMPLETE.equals(gRet_box.getBox_stat())) {
			rtnCode = updateWoCount();
			if (rtnCode != _NORMAL) {
				return rtnCode;
			}
		}

		// Check for auto goto wip bank
		if (!PROC_COMPLETE.equals(gRet_box.getBox_stat())) {
			rtnCode = gotoWipBank();
			if (rtnCode != _NORMAL) {
				return rtnCode;
			}
		}

		return rtnCode;
	}

	/**
	 * Check inTrx items
	 *
	 * @return
	 */
	private long checkInTtx() {
		long rtnCode = _NORMAL;

		if (StringUtils.isEmpty(inTrx.getBox_id())) {
			gszMsgBuf = "箱号[" + inTrx.getBox_id() + "]不能为空！";
			return E_XPMOVEOUT_INPUT_ITEM_EMPT;
		}
		if (StringUtils.isEmpty(inTrx.getTool_id())) {
			gszMsgBuf = "设备ID[" + inTrx.getTool_id() + "]不能为空！";
			return E_XPMOVEOUT_INPUT_ITEM_EMPT;
		}
		if (StringUtils.isEmpty(inTrx.getEvt_usr())) {
			gszMsgBuf = "用户名[" + inTrx.getEvt_usr() + "]不能为空！";
			return E_XPMOVEOUT_INPUT_ITEM_EMPT;
		}

		return rtnCode;
	}


	private long checkLotInfo() {
		if (StringUtils.isEmpty(inTrx.getLot_id())) {
			return _NORMAL;
		}
		Ret_prd_lotId pk = new Ret_prd_lotId();
		pk.setLot_id(inTrx.getLot_id());
		pk.setLot_splt_id(LOT_SPLIT_0);
		Ret_prd_lot ret_prd_lot = retPrdLotDao.get(pk);
		if (ret_prd_lot == null) {
			return _NORMAL;
		} else {
			if (!ret_prd_lot.getWo_id_fk().equals(gRet_box.getWo_id_fk())) {
				gszMsgBuf = "批次对应的内部订单[" + ret_prd_lot.getWo_id_fk()
						+ "]与箱子对应的内部订单[" + gRet_box.getWo_id_fk() + "]不符,请检查！";
				return E_XPMOVEOUT_WO_UNMATCH;
			}
		}
		return _NORMAL;
	}

	private long checkRwkRoute(){
		for (Ret_prd_info prd_info : gRet_prd_infoList) {
			String ope_id = prd_info.getCr_ope_id_fk();
			String ope_ver = prd_info.getCr_ope_ver_fk();
			if (!StringUtils.isEmpty(inTrx.getSwh_path_id())) {
				Bis_pathiId pathiId = new Bis_pathiId();
				pathiId.setPath_id(inTrx.getSwh_path_id());
				pathiId.setPath_ver(inTrx.getSwh_path_ver());

				Bis_opeId opeId = new Bis_opeId();
				opeId.setOpe_id(ope_id);
				opeId.setOpe_ver(ope_ver);

				Bis_ope shtOpe = bisOpeDao.get(opeId);

				Bis_pathi pathi = bisPathiDao.get(pathiId);
				String firstOpeNo = pathi.getFirst_ope_no();
				String hql3 = "FROM Bis_path where 1=1";
				hql3 += "and id.path_id_fk= '" + inTrx.getSwh_path_id() + "'";
				hql3 += " and id.path_ver_fk= '" +inTrx.getSwh_path_ver() +"'";
				hql3 += " and id.cr_ope_no= '" + firstOpeNo+"'";
				List<Bis_path> bisPathList = bisPathDao.find(hql3);
				String cr_ope_id_fk= bisPathList.get(0).getCr_ope_id_fk();
				bisPathList.get(0).getCr_ope_ver_fk();

				String hql = "FROM Bis_ope where ope_id = '" + cr_ope_id_fk + "'";

				List<Bis_ope> pathFirstOpes = bisOpeDao.find(hql);

				if (pathFirstOpes.isEmpty()) {
					gszMsgBuf = "站点[" + pathi.getFirst_ope_no() +
							"]没找到";
					return E_RET_WO + E_READ_NOT_FOUND;
				}
				if (shtOpe.getStd_ope_time() != null && shtOpe.getStd_ope_time() == 1) {
					if (pathFirstOpes.get(0).getStd_ope_time() != 1) {
						gszMsgBuf = "减薄来料检只能异转到减薄来料检";
						return E_RET_WO + E_READ_NOT_FOUND;
					}
				}else {
					String cr_path_id = prd_info.getCr_path_id_fk();
					String cr_path_ver = prd_info.getCr_path_ver_fk();
					String cr_ope_id = prd_info.getCr_ope_id_fk();
					String cr_ope_ver = prd_info.getCr_ope_ver_fk();
					String cr_ope_no = prd_info.getCr_ope_no_fk();

					String hql1 = "FROM Bis_path where path_id_fk = '" + cr_path_id +
							"' and path_ver_fk = '" + cr_path_ver +
							"' and cr_ope_id_fk = '" + cr_ope_id +
							"' and cr_ope_ver_fk = '" + cr_ope_ver + "'";

					List<Bis_path> paths = new ArrayList<Bis_path>();
					paths = bisPathDao.find(hql1);

					if (paths.isEmpty()) {
						gszMsgBuf = "该工艺线路["+cr_path_id+
								"]中的站点["+cr_ope_id+
								"在bis_path表中找不到";
						return E_RET_WO + E_READ_NOT_FOUND;
					}
					if("FRST".equals(paths.get(0).getPv_ope_no())){
						return _NORMAL;
					}else{
						String hql2 = "FROM Bis_path where path_id_fk = '" + cr_path_id +
								"' and path_ver_fk = '" + cr_path_ver +
								"' and nx_ope_no = '" + cr_ope_no + "'";

						List<Bis_path> paths2 = new ArrayList<Bis_path>();
						paths2 = bisPathDao.find(hql2);

						if (paths2.isEmpty()) {
							gszMsgBuf = "该工艺线路["+cr_path_id+
									"]中的站点["+cr_ope_no+
									"在bis_path表中找不到";
							return E_RET_WO + E_READ_NOT_FOUND;
						}
						if(pathi.getFirst_ope_no().equals(paths2.get(0).getCr_ope_id_fk())){
							return _NORMAL;
						}else {
//							gszMsgBuf = "只能异转到主工艺线路前一站";
//							return E_RET_WO + E_READ_NOT_FOUND;
						}
					}
				}
			}
		}

		return _NORMAL;
	}

	/**
	 * Mtrl back check
	 *
	 * @return
	 */
	private long mtrlBoxCheck() {
		SimpleRtnBeen rtnBeen = new SimpleRtnBeen(_NORMAL);
		Bis_ope bis_ope = new Bis_ope();
		Bis_opeId bis_opeId = new Bis_opeId();
		Ret_wo ret_wo = new Ret_wo();

		// Check wo
		ret_wo = retWODao.get(gRet_prd_infoList.get(0).getWo_id_fk());
		if (null == ret_wo) {
			gszMsgBuf = new NotFoundRtnBeen(gRet_prd_infoList.get(0)
					.getWo_id_fk(), Ret_wo.class).toString();
			return E_RET_WO + E_READ_NOT_FOUND;
		}else{
			gCus_id = ret_wo.getCus_id_fk();
			gWo_id = ret_wo.getWo_id();
			gSo_id = ret_wo.getSo_id();
			gMdl_cate = ret_wo.getMdl_cate_fk();
			gTthickness = String.valueOf(ret_wo.getT_thickness());
			gCthickness = String.valueOf(ret_wo.getC_thickness());
			gMdl_id = ret_wo.getMdl_id_fk();
			plant_id =ret_wo.getPlant_id();
		}

		if (!_YES.equals(ret_wo.getOpe_eff_flg())) {
			return _NORMAL;
		}

		// Check Ope
		bis_opeId.setOpe_id(gRet_prd_infoList.get(0).getCr_ope_id_fk());
		bis_opeId.setOpe_ver(gRet_prd_infoList.get(0).getCr_ope_ver_fk());
		bis_ope = bisOpeDao.get(bis_opeId);
		if (null == bis_ope) {
			gszMsgBuf = new NotFoundRtnBeen(bis_opeId.toString(), Bis_ope.class)
					.toString();
			return E_BIS_OPE + E_READ_NOT_FOUND;
		}

		if (!_YES.equals(bis_ope.getYield_chk_flg())) {
			return _NORMAL;
		}

		// All check ok, check mtrl box
		rtnBeen = retBoxDao.chkMtrlBoxAndSlot(gRet_box, true);
		if (_NORMAL != rtnBeen.getlRc()) {
			gszMsgBuf = rtnBeen.getMsgBuf();
			return rtnBeen.getlRc();
		}

		return _NORMAL;
	}

	// Get [DATA-PRST] by proc
	private long getPRST() {
		// gBis_data_PRST
		if (StringUtils.isEmpty(gRet_prd_infoList.get(0).getCr_proc_id_fk())) {
			return _NORMAL;// At last ope
		}
		String queryPRST = "FROM Bis_data WHERE data_cate='PRST' AND data_id = '"
				+ gRet_prd_infoList.get(0).getCr_proc_id_fk() + "'";
		gBis_data_PRST = (Bis_data) bisDataDao.comTblRead(queryPRST);
		if (null == gBis_data_PRST) {
			gszMsgBuf = new NotFoundRtnBeen("参数：'PRST'  "
					+ gRet_prd_infoList.get(0).getCr_proc_id_fk(),
					Bis_data.class).toString();
			return E_BIS_DATA + E_READ_NOT_FOUND;
		}else{
			gProc_id = gBis_data_PRST.getData_id();
			gOpe_dsc = gBis_data_PRST.getData_desc();
		}

		return _NORMAL;
	}

	/**
	 * Add lot info, just UPK use
	 *
	 * @return
	 */
	private long addLotInfo() {
		if (!StringUtils.isEmpty(inTrx.getLot_id())) {
			Ret_prd_lot ret_prd_lot = new Ret_prd_lot();
			Ret_prd_lotId ret_prd_lotId = new Ret_prd_lotId();
			ret_prd_lotId.setLot_id(inTrx.getLot_id());
			ret_prd_lotId.setLot_splt_id(LOT_SPLIT_0);

			ret_prd_lot = retPrdLotDao.get(ret_prd_lotId);
			if (null != ret_prd_lot) {
				ret_prd_lot.setLot_qty(prdLotCnt + ret_prd_lot.getLot_qty());

				ret_prd_lot.setEvt_usr(inTrx.getEvt_usr());
				ret_prd_lot.setEvt_timestamp(gEvtTimestamp);
				ret_prd_lot.setEvt_time_d(gEvtTimeD);

				retPrdLotDao.update(ret_prd_lot);
			} else {
				ret_prd_lot = new Ret_prd_lot();
				ret_prd_lotId = new Ret_prd_lotId();
				ret_prd_lotId.setLot_id(inTrx.getLot_id());
				ret_prd_lotId.setLot_splt_id(LOT_SPLIT_0);
				ret_prd_lot.setId(ret_prd_lotId);
				ret_prd_lot.setLot_stat(PROC_WAIT);
				ret_prd_lot.setLot_qty(prdLotCnt);
				ret_prd_lot.setWo_id_fk(gRet_box.getWo_id_fk());// 增加内部订单

				ret_prd_lot.setEvt_usr(inTrx.getEvt_usr());
				ret_prd_lot.setEvt_timestamp(gEvtTimestamp);
				ret_prd_lot.setEvt_time_d(gEvtTimeD);

				ret_prd_lot.setStb_timestamp(gEvtTimestamp);

				retPrdLotDao.save(ret_prd_lot);
			}
		}

		return _NORMAL;
	}

	/**
	 * Get tool info
	 *
	 * @return
	 */
	private long getToolInfo() {
		String toolID = inTrx.getTool_id();
		gBis_tool = bisToolDao.get(toolID);
		if (null == gBis_tool) {
			gszMsgBuf = new NotFoundRtnBeen(toolID, Bis_tool.class).toString();
			return E_BIS_TOOL_GR + E_READ_NOT_FOUND;
		} else {
			if (!TOOL_UNIT_MAIN.equals(gBis_tool.getUnit_typ())) {
				gszMsgBuf = "设备 [" + toolID + "] 类型不正确 ["
						+ gBis_tool.getUnit_typ() + "], 必须是 [" + TOOL_UNIT_MAIN
						+ "] type!";
				return E_XPMOVEOUT_TOOL_UNIT_TYPE_INVALIED;
			}
		}

		/**
		 * 不在检查机台状态是否为DOWN
		 */
//		gRet_tool_st = retToolStDao.get(toolID);
//		if (null == gRet_tool_st) {
//			gszMsgBuf = new NotFoundRtnBeen(toolID, Ret_tool_st.class)
//					.toString();
//			return E_BIS_TOOL_GR + E_READ_NOT_FOUND;
//		} else {
//			if (TOOL_STAT_DOWN.equals(gRet_tool_st.getTool_stat())
//					|| TOOL_STAT_ENG.equals(gRet_tool_st.getTool_stat())) {
//				gszMsgBuf = "设备 [" + toolID + "] 状态不正确 ["
//						+ gRet_tool_st.getTool_stat() + "]!";
//				return E_XPMOVEOUT_TOOL_STATUS_INVALIED;
//			}
//		}

		String sqlGetToolGrp = "FROM Bis_tool_gr WHERE id.tool_id_fk='"
				+ toolID + "'";
		gBis_tool_gr = (Bis_tool_gr) bisToolGrDao.comTblRead(sqlGetToolGrp);
		if (null == gBis_tool_gr) {
			gszMsgBuf = new NotFoundRtnBeen(toolID, Bis_tool_gr.class)
					.toString();
			return E_BIS_TOOL_GR + E_READ_NOT_FOUND;
		}

		return _NORMAL;
	}

	/**
	 * Get box info
	 *
	 * @return
	 */
	private long getRetBoxInfo() {
		String boxID = inTrx.getBox_id();
		gRet_box = retBoxDao.getWithLock(boxID, LockMode.UPGRADE);
		if (null == gRet_box) {
			gszMsgBuf = new NotFoundRtnBeen(boxID, Ret_box.class).toString();
			return E_RET_BOX + E_READ_NOT_FOUND;
		}
		// 检查箱子是否在制保留
		if (WIP_REATIN.equals(gRet_box.getBnk_flg())) {
			gszMsgBuf = "箱子的bnk_flg为 [" + gRet_box.getBnk_flg()
					+ "],已在制保留，请先取消在制保留！";
			return E_XPMOVEOUT_INVALIED_BNK_FLG;
		}

		// Check status
		if (!PROC_EMPTY.equals(gRet_box.getBox_stat())
				|| EMPTY_CARRIER.equals(gRet_box.getReal_emp())) {
			gszMsgBuf = "箱子 [" + boxID + "] 状态是 [" + gRet_box.getBox_stat()
					+ "][" + gRet_box.getReal_emp() + "], 必须是 [" + PROC_EMPTY
					+ "][" + EXIST_CARRIER + "]!";
			return E_XPMOVEIN_BOX_STATUS_INVALIED;
		}

		return _NORMAL;
	}
	/**
	 * check 007
	 *
	 * @return
	 */
	private long check007Info() {
		String boxID = inTrx.getBox_id();
		List<Ret_prd_info> Ret_prd_infoList = retPrdInfoDao.getShtListInfo(boxID);
		if (null == Ret_prd_infoList) {
			gszMsgBuf = "箱子 [" + boxID + "] 中没有玻璃!";
			return E_RET_PRD_INFO + E_READ_NOT_FOUND;
		}
		Ret_wo ret_wo = new Ret_wo();

		// Check wo
		ret_wo = retWODao.get(Ret_prd_infoList.get(0).getWo_id_fk());
		if (null == ret_wo) {
			gszMsgBuf = new NotFoundRtnBeen(Ret_prd_infoList.get(0)
					.getWo_id_fk(), Ret_wo.class).toString();
			return E_RET_WO + E_READ_NOT_FOUND;
		}
		for (int i = 0; i < Ret_prd_infoList.size(); i++) {
			if(_YES.equals(ret_wo.getSwh_cnt_flg())||"007".equals(ret_wo.getCus_id_fk())){//客户来料标记
				if(!Ret_prd_infoList.get(0).getGroup_id().equals(Ret_prd_infoList.get(i).getGroup_id())){
					gszMsgBuf = "玻璃的组代码不能混！[" + Ret_prd_infoList.get(0).getPrd_seq_id()+ "]["
							+ Ret_prd_infoList.get(0).getGroup_id() + "] vs ["
							+ Ret_prd_infoList.get(i).getPrd_seq_id() + "]["
							+ Ret_prd_infoList.get(i).getGroup_id() + "]";
					return E_XPMOVEOUT_OUT_TOOLG_UNMATCH;
				}
				if(!Ret_prd_infoList.get(0).getPrd_grade().equals(Ret_prd_infoList.get(i).getPrd_grade())){
					gszMsgBuf = "玻璃的等级不能混！[" + Ret_prd_infoList.get(0).getPrd_seq_id()+ "]["
							+ Ret_prd_infoList.get(0).getPrd_grade() + "] vs ["
							+ Ret_prd_infoList.get(i).getPrd_seq_id() + "]["
							+ Ret_prd_infoList.get(i).getPrd_grade() + "]";
					return E_XPMOVEOUT_OUT_TOOLG_UNMATCH;
				}
			}
		}
		return _NORMAL;
	}

	/**
	 * Get sht list info by box
	 *
	 * @return
	 */
	private long getPrdListInfo() {
		String boxID = inTrx.getBox_id();
		gRet_prd_infoList = retPrdInfoDao.getShtListInfo(boxID);
		if (null == gRet_prd_infoList) {
			gszMsgBuf = "箱子 [" + boxID + "] 中没有玻璃!";
			return E_RET_PRD_INFO + E_READ_NOT_FOUND;
		}
		String sql = "FROM Ret_prd_info WHERE pv_box_id_fk='"+gRet_prd_infoList.get(0).getPv_box_id_fk()+"' AND cr_proc_id_fk='"+gRet_prd_infoList.get(0).getCr_proc_id_fk()+"' AND wo_id_fk='"+gRet_prd_infoList.get(0).getWo_id_fk()+"'";
		gRet_prd_infoList_movein = retPrdInfoDao.find(sql);

		return _NORMAL;
	}

	/**
	 * Check sht list(status & tool)
	 *
	 * @return
	 */
	private long chkShtList() {
		// Check total count
		if (gRet_prd_infoList.size() != gRet_box.getPrd_qty()) {
			gszMsgBuf = "箱子显示数量 [" + gRet_box.getPrd_qty() + "] 和实际查询到的玻璃数量["
					+ gRet_prd_infoList.size() + "] 不匹配!";
			return E_XPMOVEOUT_BOX_AND_PRD_COUNT_NOT_MATCH;
		}
		if(_YES.equals(inTrx.getCntflg())){
			if(gRet_prd_infoList.size()!=gRet_prd_infoList_movein.size()){
				gszMsgBuf = "出账玻璃数量 [" + gRet_prd_infoList.size() + "] 和入账玻璃数量["
						+ gRet_prd_infoList_movein.size() + "] 不匹配!";
				return E_XPMOVEOUT_BOX_AND_PRD_COUNT_NOT_MATCH;
			}
		}

		// Check mix
		for (int i = 0; i < gRet_prd_infoList.size(); i++) {
			// Status must INPR
			if (!PROC_IN_PROCESS.equals(gRet_prd_infoList.get(i).getPrd_stat())) {
				gszMsgBuf = "玻璃 [" + gRet_prd_infoList.get(i).getPrd_seq_id()
						+ "] 状态是[" + gRet_prd_infoList.get(i).getPrd_stat()
						+ "] 必须是 [" + PROC_IN_PROCESS + "]!";
				return E_XPMOVEOUT_SHT_STATUS_INVALIED;
			}

			// 检查产品是否在制保留
			if (WIP_REATIN.equals(gRet_prd_infoList.get(i).getBnk_flg())) {
				gszMsgBuf = "产品的bnk_flg为 ["
						+ gRet_prd_infoList.get(i).getBnk_flg()
						+ "],已在制保留，请先取消在制保留！";
				return E_XPMOVEOUT_INVALIED_BNK_FLG;
			}

			// Check Tool
			if (!gBis_tool_gr.getId().getToolg_id()
					.equals(gRet_prd_infoList.get(i).getCr_toolg_id_fk())) {
				gszMsgBuf = "玻璃 [" + gRet_prd_infoList.get(i).getPrd_seq_id()
						+ "]的设备组是["
						+ gRet_prd_infoList.get(i).getCr_toolg_id_fk()
						+ "] ，应该是 [" + gBis_tool_gr.getId().getToolg_id()
						+ "]!";
				return E_XPMOVEOUT_OUT_TOOLG_UNMATCH;
			}

		}
		return _NORMAL;
	}

	/**
	 * Check slot for move out
	 *
	 * @return
	 */
	private long chkSlotNo() {
		String slotNO1 = "";
		for (int i = 0; i < gRet_prd_infoList.size(); i++) {
			slotNO1 = gRet_prd_infoList.get(i).getSlot_no();

			if (!(Integer.valueOf(slotNO1) > 0)) {
				gszMsgBuf = "玻璃 [" + gRet_prd_infoList.get(i).getPrd_seq_id()
						+ "]的子格位 [" + slotNO1 + "] 无效！ ";
				return SLOT_NO_INVALIED;
			}
		}

		return _NORMAL;
	}

	/**
	 * Move out
	 *
	 * @return
	 */
	private long moveOutProc() {
		long rtnCode = _NORMAL;

		rtnCode = realMoveOutProc();
		if (rtnCode != _NORMAL) {
			return rtnCode;
		}

		return _NORMAL;
	}

	/**
	 * Change lot id for JBQA, Use box_id as lot_id
	 *
	 * @return
	 */
	private long lotChangeProc() {
		long rtnCode = _NORMAL;
		String nx_proc_id = gRet_prd_infoList.get(0).getNx_proc_id_fk();
		if (!"DMIC".equals(nx_proc_id)) {
			return rtnCode;
		}
		if(!StringUtils.isEmpty(inTrx.getQa_back_flg()) && inTrx.getQa_back_flg().equals("Y")){
			return rtnCode;
		}
		//只有第一次会change lot
		if(!StringUtils.isEmpty(gRet_prd_infoList.get(0).getSnd_lot_id_fk())){
			return rtnCode;
		}
		// Chnage Lot ID
		String boxID = gRet_prd_infoList.get(0).getBox_id_fk();
		for (int i = 0; i < gRet_prd_infoList.size(); i++) {
			gRet_prd_infoList.get(i).setLot_id(boxID);
			gRet_prd_infoList.get(i).setSnd_lot_id_fk(boxID);

			gRet_prd_infoList.get(i).setEvt_cate(EVT_CHNAGE_LOT);
			gRet_prd_infoList.get(i).setEvt_timestamp(gEvtTimestamp);
			gRet_prd_infoList.get(i).setEvt_time_d(gEvtTimeD);
			gRet_prd_infoList.get(i).setEvt_seq_id(gEvtSeqID);
			gRet_prd_infoList.get(i).setEvt_usr(inTrx.getEvt_usr());
			gRet_prd_infoList.get(i).setExt_3(inTrx.getJudge_user());
			retPrdInfoDao.update(gRet_prd_infoList.get(i));
		}

		// Add lot
		Ret_prd_lot ret_prd_lot = new Ret_prd_lot();
		Ret_prd_lotId ret_prd_lotId = new Ret_prd_lotId();
		ret_prd_lotId.setLot_id(boxID);
		ret_prd_lotId.setLot_splt_id(LOT_SPLIT_0);

		ret_prd_lot = retPrdLotDao.get(ret_prd_lotId);
		if(ret_prd_lot == null){
			ret_prd_lot = new Ret_prd_lot();
			ret_prd_lotId = new Ret_prd_lotId();
			ret_prd_lotId.setLot_id(boxID);
			ret_prd_lotId.setLot_splt_id(LOT_SPLIT_0);
			ret_prd_lot.setId(ret_prd_lotId);
			ret_prd_lot.setLot_stat(PROC_WAIT);
			ret_prd_lot.setLot_qty(gRet_prd_infoList.size());
			ret_prd_lot.setWo_id_fk(gRet_prd_infoList.get(0).getWo_id_fk());

			ret_prd_lot.setEvt_usr(inTrx.getEvt_usr());
			ret_prd_lot.setEvt_timestamp(gEvtTimestamp);
			ret_prd_lot.setEvt_time_d(gEvtTimeD);

			ret_prd_lot.setStb_timestamp(gEvtTimestamp);

			retPrdLotDao.save(ret_prd_lot);
		}

		return rtnCode;
	}

	/**
	 * Real move out process
	 *
	 * @return
	 */
	private long realMoveOutProc() {
		String rwk_Flg = "N";
		String nx_pep_lvl;
		StringBuffer cus_sn;
		int index = 0;
		boolean dps_calc_flg = false;
		SimpleRtnBeen rtnBeen = new SimpleRtnBeen(_NORMAL);


		for (Integer prdCnt = 0; prdCnt < gRet_prd_infoList.size(); prdCnt++) {
			rtnBeen = shtMoveOutProc(gRet_prd_infoList.get(prdCnt));
			if (rtnBeen.getlRc() != _NORMAL) {
				return rtnBeen.getlRc();
			}
			// Rework for select
			if (!StringUtils.isEmpty(inTrx.getSwh_path_id())) {
				//如果异转站点就是当前站点，报错
				if (gRet_prd_infoList.get(prdCnt).getCr_path_id_fk().equals(inTrx.getSwh_path_id()) &&
						gRet_prd_infoList.get(prdCnt).getCr_path_ver_fk().equals(inTrx.getSwh_path_ver())) {
					gszMsgBuf = "玻璃 ["
							+ gRet_prd_infoList.get(prdCnt).getPrd_seq_id()
							+ "], 已经在重工工艺路线 ["
							+ gRet_prd_infoList.get(prdCnt).getCr_path_id_fk()
							+ "], 不用再异转";
					return E_XPMOVEOUT_REWK_PARENT_INVALID;
				}


				/**
				 * 1、查询OUT站点是否在产品所在的途程中
				 * 2、如果异转的途程（Rework）只有一个站点，
				 * 	     那么对ret_prd_out表中记录做更新，而不是插入
				 *   （更新字段：to_path_id，to_path_ver）
				 */

				String hql = "FROM Bis_path where path_id_fk = '" + gRet_prd_infoList.get(prdCnt).getCr_path_id_fk() +
						"' and path_ver_fk = '" + gRet_prd_infoList.get(prdCnt).getCr_path_ver_fk() + "'";

				List<Bis_path> pathList = bisPathDao.find(hql);

				if (pathList.isEmpty()) {
					gszMsgBuf = "该途程[" + gRet_prd_infoList.get(prdCnt).getCr_path_id_fk()
							+ "]不存在";
					return E_BIS_PATH + E_READ_NOT_FOUND;
				}

				String hql1 = "FROM Ret_prd_out WHERE prd_seq_id_fk = '" + gRet_prd_infoList.get(prdCnt).getPrd_seq_id() +
						"' AND to_path_id_fk = '" + gRet_prd_infoList.get(prdCnt).getCr_path_id_fk() +
						"' AND to_path_ver_fk = '" + gRet_prd_infoList.get(prdCnt).getCr_path_ver_fk() + "'";

				List<Ret_prd_out> ret_prd_out_list = retPrdOutDao.find(hql1);

				logger.info(StringUtils.isEmpty(gRet_prd_infoList.get(prdCnt).getNx_ope_id_fk()));
				logger.info(gRet_prd_infoList.get(prdCnt).getNx_ope_id_fk());

				if (ret_prd_out_list.isEmpty() || pathList.size() > 1) {
					rtnBeen = addReworkOutSetting2(gRet_prd_infoList.get(prdCnt),
							gBis_path_rwk);
					if (rtnBeen.getlRc() != _NORMAL) {
						return rtnBeen.getlRc();
					}
				}else {
					rtnBeen = updateReworkOutSetting2(gRet_prd_infoList.get(prdCnt),
							gBis_path_rwk);
					if (rtnBeen.getlRc() != _NORMAL) {
						return rtnBeen.getlRc();
					}
				}



				// 修改Prd info
				rtnBeen = shtGotoReworkProc(gRet_prd_infoList.get(prdCnt));
				if (rtnBeen.getlRc() != _NORMAL) {
					return rtnBeen.getlRc();
				}
				gRet_prd_infoList.set(prdCnt, (Ret_prd_info) rtnBeen.getObj());
			} else {
				gRet_prd_infoList.set(prdCnt, (Ret_prd_info) rtnBeen.getObj());
			}

			//增加返工品计算逻辑
			if(!gRet_prd_infoList.get(prdCnt).getPrd_stat().equals(PROC_COMPLETE)){
				if(prdCnt == 0){
					Bis_pathiId bis_pathiId = new Bis_pathiId();
					Bis_pathi nx_bis_pathI = new Bis_pathi();
					Bis_opeId nx_bis_opeId = new Bis_opeId();
					Bis_ope nx_bis_ope = new Bis_ope();

					bis_pathiId.setPath_id(gRet_prd_infoList.get(0).getNx_path_id_fk());
					bis_pathiId.setPath_ver(gRet_prd_infoList.get(0).getNx_path_ver_fk());
					nx_bis_pathI = bisPathiDao.get(bis_pathiId);
					if(nx_bis_pathI.getPath_cate().equals(PATH_TYPE_REWK)){
						rwk_Flg = "Y";
						gRet_prd_infoList.get(0).setOqc_skip_flg("Y");
					}else{
						nx_bis_opeId.setOpe_id(gRet_prd_infoList.get(0).getNx_ope_id_fk());
						nx_bis_opeId.setOpe_ver(gRet_prd_infoList.get(0).getNx_ope_ver_fk());

						nx_bis_ope = bisOpeDao.get(nx_bis_opeId);
						nx_pep_lvl = nx_bis_ope.getPep_lvl();
						index = Integer.valueOf(nx_pep_lvl) - 1;

						cus_sn = new StringBuffer(gRet_prd_infoList.get(0).getCus_sn());
						int count = 0 ;
//						try {
//							count = Util.S2I(cus_sn.substring(index, index+1));
//						} catch (NumberFormatException e) {
//							count = 1 ;
//						}

						if (count == 0) {
							gRet_prd_infoList.get(0).setOqc_skip_flg("N");
						}else {
							gRet_prd_infoList.get(0).setOqc_skip_flg("Y");
						}
					}

				}else{
					if(rwk_Flg.equals("Y")){
						gRet_prd_infoList.get(prdCnt).setOqc_skip_flg("Y");
					}else{
						cus_sn = new StringBuffer(gRet_prd_infoList.get(prdCnt).getCus_sn());
						int count = 0 ;
//						try {
//							count = Util.S2I(cus_sn.substring(index, index+1));
//						} catch (NumberFormatException e) {
//							count = 1 ;
//						}
//
						if (count == 0) {
							gRet_prd_infoList.get(prdCnt).setOqc_skip_flg("N");
						}else {
							gRet_prd_infoList.get(prdCnt).setOqc_skip_flg("Y");
						}
					}
				}

			}
			retPrdInfoDao.update(gRet_prd_infoList.get(prdCnt));

		}

		return _NORMAL;
	}

	/**
	 * Process every sht for move out
	 *
	 * @param prd
	 * @return
	 */
	private SimpleRtnBeen shtMoveOutProc(Ret_prd_info prd) {
		SimpleRtnBeen rtnBeen = new SimpleRtnBeen(_NORMAL);
		String mdl_cate = gBis_data_PRST.getExt_4();

		if(StringUtils.isEmpty(prd.getPv_prd_grade())){
			prd.setPv_prd_grade(_OK);
		}
		if(StringUtils.isEmpty(prd.getPrd_grade())){
			prd.setPrd_grade(prd.getPv_prd_grade());
		}else{
			prd.setPv_prd_grade(prd.getPrd_grade());
		}

		// Event
		prd.setEvt_cate(EVT_LGOF);
		prd.setEvt_timestamp(gEvtTimestamp);
		if(prd.getCr_proc_id_fk().equals("JBQA")){
			prd.setP3_timestamp(gEvtTimestamp);
		}
		prd.setEvt_time_d(gEvtTimeD);
		prd.setEvt_seq_id(gEvtSeqID);
		prd.setEvt_usr(inTrx.getEvt_usr());
		prd.setPv_logof_usr(prd.getLogof_usr());
		prd.setPv_logof_timestamp(prd.getLogof_timestamp());
		prd.setLogof_usr(inTrx.getEvt_usr());
		prd.setLogof_timestamp(gEvtTimestamp);

		if(inTrx.getLot_id() != null){
			prd.setFst_lot_id_fk(inTrx.getLot_id());
		}

		String cr_ope_id = prd.getCr_ope_id_fk();
		String cr_ope_ver = prd.getCr_ope_ver_fk();

		Bis_opeId bis_opeId = new Bis_opeId();
		Bis_ope bis_ope = new Bis_ope();

		bis_opeId.setOpe_id(cr_ope_id);
		bis_opeId.setOpe_ver(cr_ope_ver);

		bis_ope = bisOpeDao.get(bis_opeId);
		String pep_lvl = bis_ope.getPep_lvl();

		int index = Integer.valueOf(pep_lvl) - 1;

		StringBuffer cus_sn = new StringBuffer(prd.getCus_sn());
		int count = 0 ;
		try {
			count = Util.S2I(cus_sn.substring(index, index+1)) + 1;
		} catch (NumberFormatException e) {
			count = 0 ;
		}

		if (count > 1) {
			prd.setProc_flg("N");
		}else {
			prd.setProc_flg("Y");
			if (bis_ope.getHand_over_flg().equals("Y")) {
				if (mdl_cate.equals("A")) {
					prd.setRsn_dept5("Y");
				}else if (mdl_cate.equals("B")) {
					prd.setRsn_cate5("Y");
				}else if (mdl_cate.equals("C")) {
					prd.setRsn_code5("Y");
				}
			}
		}

		char count_char = Util.I2S(count).charAt(0);
		cus_sn.setCharAt(index, count_char);

		prd.setCus_sn(cus_sn.toString());


		if(inTrx.getRemark()!=null){
			prd.setSht_ope_msg(inTrx.getRemark());
		}

		// set lot_id
		if (!StringUtils.isEmpty(inTrx.getLot_id())
				&& StringUtils.isEmpty(prd.getLot_id())) {
			prd.setLot_id(inTrx.getLot_id());
			prd.setLot_splt_id(LOT_SPLIT_0);
			prdLotCnt++;
		}

		rtnBeen = this.setNextOpeBySht(prd);
		if (rtnBeen.getlRc() != _NORMAL) {
			return new SimpleRtnBeen(rtnBeen.getlRc());
		} else {
			prd = (Ret_prd_info) rtnBeen.getObj();
		}

		return new SimpleRtnBeen(prd);
	}

	/**
	 * Set sht's next ope/path info for move out
	 *
	 * @param prd
	 * @return
	 */
	private SimpleRtnBeen setNextOpeBySht(Ret_prd_info prd) {
		String nxOpeNo = prd.getNx_ope_no_fk();

		if (StringUtils.isEmpty(nxOpeNo)) {
			// Find path out
			Bis_pathi crPathi = new Bis_pathi();
			Bis_pathiId bisPathId = new Bis_pathiId();

			bisPathId.setPath_id(prd.getCr_path_id_fk());
			bisPathId.setPath_ver(prd.getCr_path_ver_fk());
			crPathi = bisPathiDao.get(bisPathId);

			if (PATH_TYPE_MAIN.equals(crPathi.getPath_cate())) {
				// COMP process
				prd.setNx_path_id_fk(_SPACE);
				prd.setNx_path_ver_fk(_SPACE);
				prd.setNx_ope_no_fk(_SPACE);
				prd.setNx_ope_id_fk(_SPACE);
				prd.setNx_ope_ver_fk(_SPACE);
				prd.setNx_toolg_id_fk(_SPACE);
				prd.setNx_tool_id_fk(_SPACE);

				prd.setPv_prd_stat_fk(prd.getPrd_stat());
				prd.setPrd_stat(PROC_COMPLETE);
				prd.setEvt_cate(EVT_LGOF);
				// retPrdInfoDao.update(prd);
			} else if (PATH_TYPE_REWK.equals(crPathi.getPath_cate()) && StringUtils.isEmpty(inTrx.getSwh_path_id())) {
				Bis_path nxPath = new Bis_path();
				Bis_pathi nxPathi = new Bis_pathi();
				Bis_ope nxOpe = new Bis_ope();
				Bis_pathiId nxBisPathiId = new Bis_pathiId();
				Bis_pathId nxBisPathId = new Bis_pathId();
				Bis_opeId nxBisOpeId = new Bis_opeId();

				// Find main path
				Ret_prd_out ret_prd_out = new Ret_prd_out();

				ret_prd_out = retPrdOutDao.getPrdOutRecord(prd.getPrd_seq_id(),
						prd.getCr_path_id_fk(), prd.getCr_path_ver_fk());

				if (null == ret_prd_out) {
					gszMsgBuf = new NotFoundRtnBeen("玻璃 ["
							+ prd.getPrd_seq_id() + "], 工艺路线 ["
							+ prd.getCr_path_id_fk() + "], 工艺路线版本 ["
							+ prd.getCr_path_ver_fk() + "]", Ret_prd_out.class)
							.toString();
					return new SimpleRtnBeen(E_RET_PRD_OUT + E_READ_NOT_FOUND);
				}

				nxBisPathiId.setPath_id(ret_prd_out.getOut_path_id_fk());
				nxBisPathiId.setPath_ver(ret_prd_out.getOut_path_ver_fk());
				nxPathi = bisPathiDao.get(nxBisPathiId);
				if (null == nxPathi) {
					gszMsgBuf = new NotFoundRtnBeen(nxBisPathiId.toString(),
							Bis_pathi.class).toString();
					return new SimpleRtnBeen(E_BIS_PATHI + E_READ_NOT_FOUND);
				}
				nxBisPathId.setPath_id_fk(ret_prd_out.getOut_path_id_fk());
				nxBisPathId.setPath_ver_fk(ret_prd_out.getOut_path_ver_fk());
				nxBisPathId.setCr_ope_no(ret_prd_out.getOut_ope_no_fk());
				nxPath = bisPathDao.get(nxBisPathId);
				if (null == nxPath) {
					gszMsgBuf = new NotFoundRtnBeen(nxBisPathId.toString(),
							Bis_path.class).toString();
					return new SimpleRtnBeen(E_BIS_PATH + E_READ_NOT_FOUND);
				}
				nxBisOpeId.setOpe_id(nxPath.getCr_ope_id_fk());
				nxBisOpeId.setOpe_ver(nxPath.getCr_ope_ver_fk());
				nxOpe = bisOpeDao.get(nxBisOpeId);
				if (null == nxOpe) {
					gszMsgBuf = new NotFoundRtnBeen(nxBisOpeId.toString(),
							Bis_ope.class).toString();
					return new SimpleRtnBeen(E_BIS_OPE + E_READ_NOT_FOUND);
				}
				// Set prd
				prd.setNx_path_id_fk(nxPath.getId().getPath_id_fk());
				prd.setNx_path_ver_fk(nxPath.getId().getPath_ver_fk());
				prd.setNx_ope_no_fk(nxPath.getId().getCr_ope_no());
				prd.setNx_ope_id_fk(nxOpe.getId().getOpe_id());
				prd.setNx_ope_ver_fk(nxOpe.getId().getOpe_ver());
				prd.setNx_proc_id_fk(nxOpe.getProc_id());
				prd.setNx_toolg_id_fk(nxOpe.getToolg_id());
				prd.setNx_tool_id_fk(_SPACE);

				prd.setPv_prd_stat_fk(prd.getPrd_stat());
				prd.setPrd_stat(PROC_WAIT);
				prd.setEvt_cate(EVT_LGOF);
				retPrdOutDao.delete(ret_prd_out);
			} else if (PATH_TYPE_SAMP.equals(crPathi.getPath_cate())) {
				// Find father path.ope
				Ret_prd_out ret_prd_out = new Ret_prd_out();
				ret_prd_out = retPrdOutDao.getPrdOutRecord(prd.getPrd_seq_id(),
						prd.getCr_path_id_fk(), prd.getCr_path_ver_fk());
				// TODO Delete ret_prd_out

				Bis_pathi fromPathi = new Bis_pathi();
				Bis_pathiId fromBisPathiId = new Bis_pathiId();
				fromBisPathiId.setPath_id(ret_prd_out.getFr_path_id_fk());
				fromBisPathiId.setPath_ver(ret_prd_out.getFr_path_ver_fk());
				fromPathi = bisPathiDao.get(fromBisPathiId);
				if (null == fromPathi) {
					gszMsgBuf = new NotFoundRtnBeen(fromBisPathiId.toString(),
							Bis_pathi.class).toString();
					return new SimpleRtnBeen(E_BIS_PATHI + E_READ_NOT_FOUND);
				}

				if (PATH_TYPE_MAIN.equals(fromPathi.getPath_cate())) {
					prd.setNx_path_id_fk(prd.getSv_path_id_fk());
					prd.setNx_path_ver_fk(prd.getSv_path_ver_fk());
					prd.setNx_ope_no_fk(prd.getSv_ope_no_fk());
					prd.setNx_ope_id_fk(prd.getSv_ope_id_fk());
					prd.setNx_ope_ver_fk(prd.getSv_ope_ver_fk());

					// Clear pv info
					prd.setSv_path_id_fk(_SPACE);
					prd.setSv_path_ver_fk(_SPACE);
					prd.setSv_ope_no_fk(_SPACE);
					prd.setSv_ope_id_fk(_SPACE);
					prd.setSv_ope_ver_fk(_SPACE);
					prd.setPrd_stat(PROC_WAIT);
					prd.setPv_prd_stat_fk(prd.getPrd_stat());
					prd.setPrd_stat(PROC_WAIT);
					prd.setEvt_cate(EVT_LGOF);
				} else if (PATH_TYPE_REWK.equals(fromPathi.getPath_cate())) {
					// Get next ope in rework path.
					NxOpeAll nxOpeAllRewk = new NxOpeAll();
					nxOpeAllRewk = bisOpeDao.getNxOpeInfo(fromPathi.getId()
									.getPath_id(), fromPathi.getId().getPath_ver(),
							ret_prd_out.getFr_ope_no_fk());

					prd.setNx_path_id_fk(nxOpeAllRewk.getNxPathi().getId()
							.getPath_id());
					prd.setNx_path_ver_fk(nxOpeAllRewk.getNxPathi().getId()
							.getPath_ver());
					if (null == nxOpeAllRewk.getNxPath()) {
						prd.setNx_ope_no_fk(_SPACE);
						prd.setNx_ope_id_fk(_SPACE);
						prd.setNx_ope_ver_fk(_SPACE);
					} else {
						prd.setNx_ope_no_fk(nxOpeAllRewk.getNxPath().getId()
								.getCr_ope_no());
						prd.setNx_ope_id_fk(nxOpeAllRewk.getNxPath()
								.getCr_ope_id_fk());
						prd.setNx_ope_ver_fk(nxOpeAllRewk.getNxPath()
								.getCr_ope_ver_fk());
					}

					prd.setPv_prd_stat_fk(prd.getPrd_stat());
					prd.setPrd_stat(PROC_WAIT);
					prd.setEvt_cate(EVT_LGOF);
					return setNextOpeBySht(prd);
				} else if (PATH_TYPE_SAMP.equals(fromPathi.getPath_cate())) {
					// TODO Update cr for rework and main?
					prd.setCr_path_id_fk(ret_prd_out.getFr_path_id_fk());
					prd.setCr_path_ver_fk(ret_prd_out.getFr_path_ver_fk());
					prd.setCr_ope_no_fk(ret_prd_out.getFr_ope_no_fk());
					// TODO Clear cr_ope_id&ver ?

					// Get next ope in samp path.
					NxOpeAll nxOpeAllSamp = new NxOpeAll();
					nxOpeAllSamp = bisOpeDao.getNxOpeInfo(
							ret_prd_out.getFr_path_id_fk(),
							ret_prd_out.getFr_path_ver_fk(),
							ret_prd_out.getFr_ope_no_fk());

					prd.setNx_path_id_fk(nxOpeAllSamp.getNxPathi().getId()
							.getPath_id());
					prd.setNx_path_ver_fk(nxOpeAllSamp.getNxPathi().getId()
							.getPath_ver());
					if (null == nxOpeAllSamp.getNxPath()) {
						prd.setNx_ope_no_fk(_SPACE);
						prd.setNx_ope_id_fk(_SPACE);
						prd.setNx_ope_ver_fk(_SPACE);
					} else {
						prd.setNx_ope_no_fk(nxOpeAllSamp.getNxPath().getId()
								.getCr_ope_no());
						prd.setNx_ope_id_fk(nxOpeAllSamp.getNxPath()
								.getCr_ope_id_fk());
						prd.setNx_ope_ver_fk(nxOpeAllSamp.getNxPath()
								.getCr_ope_ver_fk());
					}

					prd.setPv_prd_stat_fk(prd.getPrd_stat());
					prd.setPrd_stat(PROC_WAIT);
					prd.setEvt_cate(EVT_LGOF);
					return setNextOpeBySht(prd);
				}
			} else if (PATH_TYPE_REWK.equals(crPathi.getPath_cate())) {

			}else {
				gszMsgBuf = "当前工艺路线类型[" + crPathi + "] 不存在! ";
				return new SimpleRtnBeen(PATH_TYPE_INVALID);
			}
		} else {
			// Just next ope
			prd.setPv_prd_stat_fk(prd.getPrd_stat());
			prd.setPrd_stat(PROC_WAIT);
			prd.setEvt_cate(EVT_LGOF);
		}

		return new SimpleRtnBeen(prd);
	}

	/**
	 * Reset box info
	 *
	 * @return
	 */
	private long configureBox() {
		SimpleRtnBeen rtnBeen = new SimpleRtnBeen(_NORMAL);
		rtnBeen = retBoxDao.configBoxInfo(gRet_box);
		if (_NORMAL != rtnBeen.getlRc()) {
			gszMsgBuf = rtnBeen.getMsgBuf();
			return rtnBeen.getlRc();
		} else {
			gRet_box = (Ret_box) rtnBeen.getObj();
		}
		/**
		 * 记录终检人员
		 */
		if(gRet_prd_infoList.size()> 0){
			if(gRet_prd_infoList.get(0).getCr_proc_id_fk().equals("JBZJ") ||
					gRet_prd_infoList.get(0).getCr_proc_id_fk().equals("DMZJ")){
				gRet_box.setExt_1(inTrx.getEvt_usr());
			}
		}

		// Set Box yield
		if (PROC_COMPLETE.equals(gRet_box.getBox_stat())) {
			gRet_box = retBoxDao.setBoxYield(gRet_box, gRet_prd_infoList);
		}
		gRet_box.setPrd_grade_fk(inTrx.getPrd_grade());
		gRet_box.setShip_box_id(gShip_box_id);
		retBoxDao.update(gRet_box);
		return _NORMAL;
	}

	/**
	 * Jump to target ope
	 *
	 * @return
	 */
	private long jumpOpeProc() {
		Bis_path toPath = new Bis_path();
		List<Bis_path> toPathList = new ArrayList<Bis_path>();
		Bis_ope toBis_ope = new Bis_ope();
		Bis_opeId toBis_opeId = new Bis_opeId();

		// Get target path
		String queryToPath = "FROM Bis_path where id.path_id_fk ='"
				+ gRet_prd_infoList.get(0).getNx_path_id_fk()
				+ "' AND id.path_ver_fk = '"
				+ gRet_prd_infoList.get(0).getNx_path_ver_fk()
				+ "' AND cr_ope_id_fk = '" + inTrx.getJump_ope_id()
				+ "' AND cr_ope_ver_fk = '" + inTrx.getJump_ope_ver() + "' ";
		toPathList = bisPathDao.find(queryToPath);
		if (toPathList.isEmpty()) {
			gszMsgBuf = "异常转出站点 [" + inTrx.getJump_ope_id() + "]["
					+ inTrx.getJump_ope_ver() + "] 在当前工艺路线 ["
					+ gRet_prd_infoList.get(0).getNx_path_id_fk() + "]["
					+ gRet_prd_infoList.get(0).getNx_path_ver_fk() + "]中不存在";
			return E_XPMOVEOUT_JUMP_TO_OPE_NOT_EXIST;
		}

		toPath = toPathList.get(0);

		// Get target ope
		toBis_opeId.setOpe_id(toPath.getCr_ope_id_fk());
		toBis_opeId.setOpe_ver(toPath.getCr_ope_ver_fk());
		toBis_ope = bisOpeDao.get(toBis_opeId);
		if (null == toBis_ope) {
			gszMsgBuf = new NotFoundRtnBeen(toBis_opeId.toString(),
					Bis_ope.class).toString();
			return E_BIS_OPE + E_READ_NOT_FOUND;
		}

		// Set prd info
		for (int i = 0, max = gRet_prd_infoList.size(); i < max; i++) {
			gRet_prd_infoList.get(i).setCr_path_id_fk(
					gRet_prd_infoList.get(i).getNx_path_id_fk());
			gRet_prd_infoList.get(i).setCr_path_ver_fk(
					gRet_prd_infoList.get(i).getNx_path_ver_fk());
			gRet_prd_infoList.get(i).setCr_ope_no_fk(
					gRet_prd_infoList.get(i).getNx_ope_no_fk());
			gRet_prd_infoList.get(i).setCr_ope_id_fk(
					gRet_prd_infoList.get(i).getNx_ope_id_fk());
			gRet_prd_infoList.get(i).setCr_ope_ver_fk(
					gRet_prd_infoList.get(i).getNx_ope_ver_fk());
			gRet_prd_infoList.get(i).setCr_toolg_id_fk(
					gRet_prd_infoList.get(i).getNx_toolg_id_fk());

			gRet_prd_infoList.get(i).setNx_path_id_fk(
					toPath.getId().getPath_id_fk());
			gRet_prd_infoList.get(i).setNx_path_ver_fk(
					toPath.getId().getPath_ver_fk());
			gRet_prd_infoList.get(i).setNx_ope_no_fk(
					toPath.getId().getCr_ope_no());
			gRet_prd_infoList.get(i).setNx_ope_id_fk(
					toBis_ope.getId().getOpe_id());
			gRet_prd_infoList.get(i).setNx_ope_ver_fk(
					toBis_ope.getId().getOpe_ver());
			gRet_prd_infoList.get(i).setNx_toolg_id_fk(toBis_ope.getToolg_id());
			gRet_prd_infoList.get(i).setNx_proc_id_fk(toBis_ope.getProc_id());

			gRet_prd_infoList.get(i).setEvt_cate(EVT_JUMP_OPE);
			gRet_prd_infoList.get(i).setEvt_time_d(gEvtTimeD);
			gRet_prd_infoList.get(i).setEvt_timestamp(gEvtTimestamp);
			gRet_prd_infoList.get(i).setEvt_seq_id(gEvtSeqID);

			retPrdInfoDao.update(gRet_prd_infoList.get(i));
		}

		// 入账时区分
		gRet_box.setEvt_cate(EVT_JUMP_OPE);
		gRet_box.setEvt_time_d(gEvtTimeD);
		gRet_box.setEvt_timestamp(gEvtTimestamp);
		gRet_box.setEvt_usr(inTrx.getEvt_usr());

		retBoxDao.update(gRet_box);

		return _NORMAL;
	}

	/**
	 * Rework proc: change to rework path
	 *
	 * @return
	 */
	private long reworkProc() {
		long rtnCode = _NORMAL;
		SimpleRtnBeen rtnBeen = new SimpleRtnBeen(_NORMAL);

		Bis_path_swhId bis_path_swhId = new Bis_path_swhId();
		Bis_pathiId bis_pathiId_rwk = new Bis_pathiId();
		Bis_pathId bis_pathId_rwk = new Bis_pathId();
		Bis_opeId bis_opeId_rwk = new Bis_opeId();

		bis_path_swhId.setFr_path_id_fk(gRet_prd_infoList.get(0)
				.getCr_path_id_fk());
		bis_path_swhId.setFr_path_ver_fk(gRet_prd_infoList.get(0)
				.getCr_path_ver_fk());
		bis_path_swhId.setFr_ope_no_fk(gRet_prd_infoList.get(0)
				.getCr_ope_no_fk());
		bis_path_swhId.setSwh_id(inTrx.getRwk_id());
		bis_path_swhId.setSwh_typ(SWH_TYP_REWORK);
		gBis_path_swh = bisPathSwhDao.get(bis_path_swhId);
		if (null == gBis_path_swh) {
			gszMsgBuf = new NotFoundRtnBeen(bis_path_swhId.toString(),
					Bis_path_swh.class).toString();
			return E_BIS_PATH_SWH + E_READ_NOT_FOUND;
		}

		// 查询重工途程基本信息
		bis_pathiId_rwk.setPath_id(gBis_path_swh.getSwh_path_id_fk());
		bis_pathiId_rwk.setPath_ver(gBis_path_swh.getSwh_path_ver_fk());
		gBis_pathi_rwk = bisPathiDao.get(bis_pathiId_rwk);
		if (null == gBis_pathi_rwk) {
			gszMsgBuf = new NotFoundRtnBeen(bis_pathiId_rwk.toString(),
					Bis_pathi.class).toString();
			return E_BIS_PATHI + E_READ_NOT_FOUND;
		}

		// 查询重工途程首站点
		bis_pathId_rwk.setPath_id_fk(gBis_pathi_rwk.getId().getPath_id());
		bis_pathId_rwk.setPath_ver_fk(gBis_pathi_rwk.getId().getPath_ver());
		bis_pathId_rwk.setCr_ope_no(gBis_pathi_rwk.getFirst_ope_no());
		gBis_path_rwk = bisPathDao.get(bis_pathId_rwk);
		if (null == gBis_path_rwk) {
			gszMsgBuf = new NotFoundRtnBeen(bis_pathId_rwk.toString(),
					Bis_path.class).toString();
			return E_BIS_PATH + E_READ_NOT_FOUND;
		}

		// 查询站点信息 gBis_ope_rwk
		bis_opeId_rwk.setOpe_id(gBis_path_rwk.getCr_ope_id_fk());
		bis_opeId_rwk.setOpe_ver(gBis_path_rwk.getCr_ope_ver_fk());
		gBis_ope_rwk = bisOpeDao.get(bis_opeId_rwk);
		if (null == gBis_ope_rwk) {
			gszMsgBuf = new NotFoundRtnBeen(bis_opeId_rwk.toString(),
					Bis_ope.class).toString();
			return E_BIS_OPE + E_READ_NOT_FOUND;
		}

		// 修改Prd info
		for (Integer prdCnt = 0; prdCnt < gRet_prd_infoList.size(); prdCnt++) {
			rtnBeen = shtGotoReworkProc(gRet_prd_infoList.get(prdCnt));
			if (rtnBeen.getlRc() != _NORMAL) {
				return rtnBeen.getlRc();
			}
			gRet_prd_infoList.set(prdCnt, (Ret_prd_info) rtnBeen.getObj());
			retPrdInfoDao.update(gRet_prd_infoList.get(prdCnt));

			rtnBeen = addReworkOutSetting(gRet_prd_infoList.get(prdCnt),
					gBis_path_swh, gBis_path_rwk);
			if (rtnBeen.getlRc() != _NORMAL) {
				return rtnBeen.getlRc();
			}
		}

		// Reset box info
		rtnCode = configureBox();
		if (rtnCode != _NORMAL) {
			return rtnCode;
		}

		return _NORMAL;
	}

	/**
	 * 增加重工途程返回设定：RET_PRD_OUT
	 *
	 * @param ret_prd_info
	 * @return
	 */
	private SimpleRtnBeen addReworkOutSetting(Ret_prd_info ret_prd_info,
											  Bis_path_swh gBis_path_swh, Bis_path gBis_path_rwk) {
		Ret_prd_out ret_prd_out = new Ret_prd_out();
		Ret_prd_outId ret_prd_outId = new Ret_prd_outId();

		ret_prd_outId.setPrd_seq_id_fk(ret_prd_info.getPrd_seq_id());
		ret_prd_outId.setEvt_seq_id_fk(gEvtSeqID);
		ret_prd_outId.setEvt_timestamp(gEvtTimestamp);

		ret_prd_out.setId(ret_prd_outId);
		ret_prd_out.setRul_act_code(RUL_ACT_REWORK_OUT);

		// From (Bis_path_swh)
		ret_prd_out.setFr_path_id_fk(gBis_path_swh.getId().getFr_path_id_fk());
		ret_prd_out
				.setFr_path_ver_fk(gBis_path_swh.getId().getFr_path_ver_fk());
		ret_prd_out.setFr_ope_no_fk(gBis_path_swh.getId().getFr_ope_no_fk());
		ret_prd_out.setSwh_id(gBis_path_swh.getId().getSwh_id());
		ret_prd_out.setSwh_typ(gBis_path_swh.getId().getSwh_typ());

		// To (Bis_path)
		ret_prd_out.setTo_path_id_fk(gBis_path_rwk.getId().getPath_id_fk());
		ret_prd_out.setTo_path_ver_fk(gBis_path_rwk.getId().getPath_ver_fk());
		ret_prd_out.setTo_ope_no_fk(gBis_path_rwk.getId().getCr_ope_no());

		// Out
		ret_prd_out.setOut_path_id_fk(gBis_path_swh.getOut_path_id_fk());
		ret_prd_out.setOut_path_ver_fk(gBis_path_swh.getOut_path_ver_fk());
		ret_prd_out.setOut_ope_no_fk(gBis_path_swh.getOut_ope_no_fk());

		retPrdOutDao.save(ret_prd_out);
		return new SimpleRtnBeen(ret_prd_out);
	}

	/**
	 * 增加重工途程返回设定：RET_PRD_OUT
	 *
	 * @param ret_prd_info
	 * @param gBis_path_rwk
	 * @return
	 */
	private SimpleRtnBeen addReworkOutSetting2(Ret_prd_info ret_prd_info,
											   Bis_path gBis_path_rwk) {
		Ret_prd_out ret_prd_out = new Ret_prd_out();
		Ret_prd_outId ret_prd_outId = new Ret_prd_outId();

		ret_prd_outId.setPrd_seq_id_fk(ret_prd_info.getPrd_seq_id());
		ret_prd_outId.setEvt_seq_id_fk(gEvtSeqID);
		ret_prd_outId.setEvt_timestamp(gEvtTimestamp);

		ret_prd_out.setId(ret_prd_outId);
		ret_prd_out.setRul_act_code(RUL_ACT_REWORK_OUT);

		// From (Bis_path_swh)
		ret_prd_out.setFr_path_id_fk(ret_prd_info.getCr_path_id_fk());
		ret_prd_out.setFr_path_ver_fk(ret_prd_info.getCr_path_ver_fk());
		ret_prd_out.setFr_ope_no_fk(ret_prd_info.getCr_ope_no_fk());
		ret_prd_out.setSwh_id("M");
		ret_prd_out.setSwh_typ("R");

		// To (Bis_path)
		ret_prd_out.setTo_path_id_fk(gBis_path_rwk.getId().getPath_id_fk());
		ret_prd_out.setTo_path_ver_fk(gBis_path_rwk.getId().getPath_ver_fk());
		ret_prd_out.setTo_ope_no_fk(gBis_path_rwk.getId().getCr_ope_no());

		// Out
		ret_prd_out.setOut_path_id_fk(ret_prd_info.getCr_path_id_fk());
		ret_prd_out.setOut_path_ver_fk(ret_prd_info.getCr_path_ver_fk());
		ret_prd_out.setOut_ope_no_fk(ret_prd_info.getCr_ope_no_fk());


		retPrdOutDao.save(ret_prd_out);
		return new SimpleRtnBeen(ret_prd_out);
	}

	/**
	 * 更新重工途程返回设定：RET_PRD_OUT
	 *
	 * @param ret_prd_info
	 * @param gBis_path_rwk
	 * @return
	 */
	private SimpleRtnBeen updateReworkOutSetting2(Ret_prd_info ret_prd_info,
												  Bis_path gBis_path_rwk) {
		String hql = "FROM Ret_prd_out WHERE to_path_id_fk = '" + ret_prd_info.getCr_path_id_fk() +
				"' AND to_path_ver_fk = '" + ret_prd_info.getCr_path_ver_fk() +
				"' AND prd_seq_id_fk = '" + ret_prd_info.getPrd_seq_id() + "'";
		List<Ret_prd_out> prdOutList = retPrdOutDao.find(hql);
		if (prdOutList.isEmpty()) {
			gszMsgBuf = "异转记录，异转至[" + ret_prd_info.getCr_path_id_fk()
					+ "]记录没找到";
			SimpleRtnBeen rtnBeen = new SimpleRtnBeen(E_BIS_PATH + E_READ_NOT_FOUND);
			return rtnBeen;
		}else if (prdOutList.size() > 1) {
			gszMsgBuf = "玻璃[" + ret_prd_info.getPrd_seq_id() +
					"]多次异转至[" + ret_prd_info.getCr_path_id_fk() +
					"],禁止交叉异转";
			SimpleRtnBeen rtnBeen = new SimpleRtnBeen(E_BIS_PATH + E_READ_NOT_FOUND);
			return rtnBeen;
		}else if (StringUtils.isEmpty(inTrx.getSwh_path_id())) {
			SimpleRtnBeen rtnBeen = new SimpleRtnBeen(_NORMAL);
			return rtnBeen;
		}else {
			Ret_prd_out ret_prd_out = prdOutList.get(0);

			// To (Bis_path)
			ret_prd_out.setTo_path_id_fk(gBis_path_rwk.getId().getPath_id_fk());
			ret_prd_out.setTo_path_ver_fk(gBis_path_rwk.getId().getPath_ver_fk());
			ret_prd_out.setTo_ope_no_fk(gBis_path_rwk.getId().getCr_ope_no());

			retPrdOutDao.update(ret_prd_out);
			return new SimpleRtnBeen(ret_prd_out);
		}
	}

	/**
	 * 将PRD转至重工途程
	 *
	 * @param ret_prd_info
	 * @return SimpleRtnBeen{Ret_prd_info}
	 */
	private SimpleRtnBeen shtGotoReworkProc(Ret_prd_info ret_prd_info) {
		// Event
		ret_prd_info.setEvt_cate(EVT_REWK_IN);
		ret_prd_info.setEvt_usr(inTrx.getEvt_usr());
		ret_prd_info.setEvt_timestamp(gEvtTimestamp);
		ret_prd_info.setEvt_seq_id(gEvtSeqID);

		// Next path & ope
		ret_prd_info.setPrd_stat(PROC_WAIT);
		ret_prd_info.setNx_path_id_fk(gBis_path_rwk.getId().getPath_id_fk());
		ret_prd_info.setNx_path_ver_fk(gBis_path_rwk.getId().getPath_ver_fk());
		ret_prd_info.setNx_ope_no_fk(gBis_path_rwk.getId().getCr_ope_no());
		ret_prd_info.setNx_ope_id_fk(gBis_path_rwk.getCr_ope_id_fk());
		ret_prd_info.setNx_ope_ver_fk(gBis_path_rwk.getCr_ope_ver_fk());
		ret_prd_info.setNx_proc_id_fk(gBis_ope_rwk.getProc_id());
		ret_prd_info.setNx_toolg_id_fk(gBis_ope_rwk.getToolg_id());
		ret_prd_info.setNx_tool_id_fk(_SPACE);


		return new SimpleRtnBeen(ret_prd_info);
	}

	/**
	 * Update wo's comp count
	 *
	 * @return
	 */
	private long updateWoCount() {
		Ret_wo ret_wo = new Ret_wo();
		ret_wo = retWODao.get(gRet_prd_infoList.get(0).getWo_id_fk());
		if (null == ret_wo) {
			gszMsgBuf = new NotFoundRtnBeen(gRet_prd_infoList.get(0)
					.getWo_id_fk(), Ret_wo.class).toString();
			return E_RET_WO + E_READ_NOT_FOUND;
		}

		ret_wo.setComp_prd_qty(ret_wo.getComp_prd_qty() + gRet_box.getPrd_qty());
		retWODao.update(ret_wo);

		return _NORMAL;
	}

	/**
	 * 自动转入WIP BANK
	 *
	 * @return
	 */
	private long gotoWipBank() {
		Bis_pathi bis_pathi = new Bis_pathi();
		Bis_path bis_path = new Bis_path();
		bis_path = bisPathDao.getPathOpeInfo(gRet_box.getNx_path_id_fk(),
				gRet_box.getNx_path_ver_fk(), gRet_box.getNx_ope_no_fk());
		if (null == bis_path) {
			gszMsgBuf = new NotFoundRtnBeen(
					gRet_box.getNx_path_id_fk() + gRet_box.getNx_path_ver_fk()
							+ gRet_box.getNx_ope_no_fk(), Bis_path.class)
					.toString();
			return E_BIS_PATH + E_READ_NOT_FOUND;
		}

		bis_pathi = bisPathiDao.getPathInfo(bis_path.getId().getPath_id_fk(),
				bis_path.getId().getPath_ver_fk());
		if (null == bis_pathi) {
			gszMsgBuf = new NotFoundRtnBeen(bis_path.getId().getPath_id_fk()
					+ bis_path.getId().getPath_ver_fk(), Bis_pathi.class)
					.toString();
			return E_BIS_PATHI + E_READ_NOT_FOUND;
		}

		if (!bis_pathi.getPath_cate().equals(PATH_TYPE_MAIN)) {
			return _NORMAL;
		}

		if (_YES.equals(bis_path.getWip_bank_flg())) {
			gRet_box.setBnk_flg(BANK_IN);
			gRet_box.setEvt_cate(EVT_WIP_BANK_IN);

			gRet_box.setTool_id_fk(bis_path.getDft_wip_bank_tool_id_fk());
			for (Ret_prd_info prd : gRet_prd_infoList) {
				prd.setBnk_flg(BANK_IN);
				prd.setEvt_cate(EVT_WIP_BANK_IN);

				prd.setPv_tool_id_fk(prd.getTool_id_fk());
				prd.setTool_id_fk(bis_path.getDft_wip_bank_tool_id_fk());

				retPrdInfoDao.update(prd);
			}
			retBoxDao.update(gRet_box);
		}
		return _NORMAL;
	}

	/**
	 * 1400界面F8按钮callback方法
	 * 检查是不是在制等待箱
	 * @return
	 */
	private long checkWipWaitBox() {
		if (StringUtils.isEmpty(inTrx.getWip_wait_ok())
				|| inTrx.getWip_wait_ok().equals("true")) {
			return _NORMAL;
		}
		for (Ret_prd_info prd : gRet_prd_infoList) {
			if (prd.getCr_proc_id_fk().equals("JBZJ")
					|| prd.getCr_proc_id_fk().equals("JBQA")) {
				if (!checkWipWaitJB()) {
					return _NORMAL;
				} else {
					gszMsgBuf = "箱子 [" + inTrx.getBox_id() + "]在制等待,请确认！";
					return E_XPMOVEOUT_IS_JB_WIP_WAIT;
				}
			} else if (prd.getCr_proc_id_fk().equals("DMIC")
					|| prd.getCr_proc_id_fk().equals("DMQX")
					|| prd.getCr_proc_id_fk().equals("DMGY")
					|| prd.getCr_proc_id_fk().equals("DMZJ")
					|| prd.getCr_proc_id_fk().equals("DMQA")) {
				if (!checkWipWaitDM()) {
					return _NORMAL;
				} else {
					gszMsgBuf = "箱子 [" + inTrx.getBox_id() + "]在制等待,请确认！";
					return E_XPMOVEOUT_IS_DM_WIP_WAIT;
				}
			}
		}
		return _NORMAL;
	}

	/**
	 * 1400界面F8按钮callback方法
	 * 检查是不是在制等待箱
	 * @return
	 */
	private boolean checkWipWaitJB() {
		String sql = "SELECT box_id_fk, mtrl_box_id_fk, COUNT(prd_seq_id),COUNT(DISTINCT(mtrl_box_id_fk)) FROM RET_PRD_INFO where "
				+ "box_id_fk = '" + inTrx.getBox_id() + "' group by box_id_fk";
		List<Object> list = retPrdInfoDao.findBySQL(sql);
		if (null == list) {
			return false;
		}
		Object[] obj = (Object[]) list.get(0);

		if (Integer.parseInt(obj[3].toString()) > 1) {// 混原料箱，ignore
			return false;
		} else {
			List<Ret_prd_in> ret_prd_in_list = new ArrayList<Ret_prd_in>();
			ret_prd_in_list = retPrdInDao
					.find("FROM Ret_prd_in where mtrl_box_id = '" + obj[1]
							+ "'");
			if (Integer.parseInt(obj[2].toString()) < ret_prd_in_list.get(0)
					.getPrd_proc_qty()) {
				return true;
			}
		}
		return false;
	}

	/**
	 * 1400界面F8按钮callback方法
	 * 检查是不是在制等待箱
	 * @return
	 */
	private boolean checkWipWaitDM() {
		String sql = "SELECT box_id_fk,lot_id,lot_splt_id, mtrl_box_id_fk, COUNT(prd_seq_id),COUNT(DISTINCT(lot_id)) FROM RET_PRD_INFO "
				+ "where box_id_fk = '"
				+ inTrx.getBox_id()
				+ "' group by box_id_fk";
		List<Object> list = retPrdInfoDao.findBySQL(sql);
		if (null == list) {
			return false;
		}
		Object[] obj = (Object[]) list.get(0);

		if (Integer.parseInt(obj[5].toString()) > 1) {// 混lot， ignore
			return false;
		} else {
			Ret_prd_lot ret_prd_lot = new Ret_prd_lot();
			Ret_prd_lotId ret_prd_lotId = new Ret_prd_lotId();
			ret_prd_lotId.setLot_id(obj[1].toString());
			ret_prd_lotId.setLot_splt_id(obj[2].toString());

			ret_prd_lot = retPrdLotDao.get(ret_prd_lotId);

			if (Integer.parseInt(obj[4].toString()) < ret_prd_lot.getLot_qty()) {
				return true;
			}
		}
		return false;
	}

	/**
	 * 在制等待箱子出账权限检查
	 * author：THY
	 * @return
	 */
	private long checkWipWaitUas(){
		if (StringUtils.isEmpty(inTrx.getWip_wait_ok())) {
			return _NORMAL;
		}
		if (!(inTrx.getWip_wait_ok().equals("true"))) {
			return _NORMAL;
		}else {
			String usr_id = inTrx.getEvt_usr();
			if (StringUtils.isEmpty(usr_id)) {
				gszMsgBuf = "请登录！";
				return E_XPMOVEOUT_IS_DM_WIP_WAIT;
			}else {
				String checkAdminStr = "SELECT A.EXT_1,A.DATA_EXT,B.EXT_4 FROM " +
						"(" +
						"(SELECT EXT_1,DATA_EXT FROM BIS_DATA WHERE DATA_CATE ='UFUG'AND data_ext='"+ usr_id +"') AS A " +
						"INNER JOIN " +
						"( SELECT EXT_1,EXT_4,EXT_3 FROM BIS_DATA WHERE DATA_CATE ='UFGP'AND EXT_4='TOPI'AND EXT_3='Y') AS B " +
						"ON A.EXT_1 = B.EXT_1" +
						")";
				String checkFuCoStr = "SELECT C.EXT_1,C.EXT_2,UG.EXT_4 FROM (" +
						"SELECT A.EXT_1,A.DATA_EXT,B.EXT_4 FROM " +
						"(" +
						"(SELECT EXT_1,DATA_EXT FROM BIS_DATA WHERE DATA_CATE ='UFUG'AND data_ext='"+ usr_id +"') " +
						"AS A " +
						"LEFT JOIN " +
						"( SELECT EXT_1,EXT_4  FROM BIS_DATA WHERE DATA_CATE ='UFGP'AND EXT_4='TOPI' ) AS B "+
						"ON A.EXT_1 = B.EXT_1 " +
						") )AS UG " +
						"INNER JOIN " +
						"( SELECT EXT_1,EXT_2 FROM  BIS_DATA  WHERE DATA_CATE='UFGC'AND EXT_2='F_WAIT' ) AS C " +
						"ON UG.EXT_1 = C.EXT_1";
				List<Bis_data> bisDataAdminList = bisDataDao.findBySQL(checkAdminStr);

				if (!(bisDataAdminList.isEmpty())) {
					return _NORMAL;
				}else {
					List<Bis_data> bisDataFuCoList = bisDataDao.findBySQL(checkFuCoStr);
					if (bisDataFuCoList.isEmpty()) {
						gszMsgBuf = "你没有在制等待箱子出账权限！";
						return E_XPMOVEOUT_IS_DM_WIP_WAIT;
					}
				}
			}
		}
		return _NORMAL;
	}


	private long skipQaFunc(){
		String qa_back_flg = inTrx.getQa_back_flg();
		String ope_id = "";
		String ope_ver = "";
		if (StringUtils.isEmpty(qa_back_flg) || "".equals(qa_back_flg)) {
			return _NORMAL;
		}
		String hql = "FROM Bis_pathi where path_id = '"+gRet_prd_infoList.get(0).getCr_path_id_fk()
				+"' and path_ver = '"+gRet_prd_infoList.get(0).getCr_path_ver_fk()+"'";
		List<Bis_pathi> bisPathiList = bisPathiDao.find(hql);
		for(Bis_pathi bis_pathi : bisPathiList ){
			if(!bis_pathi.getPath_cate().equals("MAIN")){
				gszMsgBuf = "非主工艺路线的产品不能进行跳站操作！";
				return E_XPMOVEOUT_INVALID_PATH_CATE;
			}
		}
		String hql_1 = "FROM Bis_path where path_id_fk = '"+gRet_prd_infoList.get(0).getCr_path_id_fk()
				+"' and path_ver_fk = '"+gRet_prd_infoList.get(0).getCr_path_ver_fk()
				+"' and nx_ope_no = '"+gRet_prd_infoList.get(0).getCr_ope_no_fk()+"'";
		List<Bis_path> bisPathList = bisPathDao.find(hql_1);
		if(bisPathList.isEmpty()){
			gszMsgBuf = "工艺路线的第一站不能进行跳站操作！";
			return E_XPMOVEOUT_INVALID_PATH_CATE;
		}
		//ope_id = gBis_pathi_rwk.getFirst_ope_no();
		String ope_no = gBis_pathi_rwk.getFirst_ope_no();
		String hql3 = "FROM Bis_path where 1=1";
		hql3 += "and id.path_id_fk= '" + gBis_pathi_rwk.getId().getPath_id() + "'";
		hql3 += " and id.path_ver_fk= '" +gBis_pathi_rwk.getId().getPath_ver() +"'";
		hql3 += " and id.cr_ope_no= '" + ope_no+"'";
		List<Bis_path> bisPathList2 = bisPathDao.find(hql3);
		ope_id= bisPathList2.get(0).getCr_ope_id_fk();

		ope_ver = "00000";
//		for(Bis_path bis_path : bisPathList ){
//			ope_id = bis_path.getCr_ope_id_fk();
//			ope_ver = bis_path.getCr_ope_ver_fk();
//			break;
//		}

		//设置箱子信息
		boolean read_flg = false;
		String nx_path_id_fk1 = "";
		String nx_path_ver_fk1 = "";
		String nx_ope_no_fk = "";
		String nx_ope_id_fk = "";
		String nx_ope_ver_fk = "";
		String nx_proc_id_fk = "";
		String nx_toolG_id_fk = "";

		for (Ret_prd_info prd_info : gRet_prd_infoList) {
			String cr_proc_id = prd_info.getCr_proc_id_fk();
			if (StringUtils.isEmpty(cr_proc_id)) {
				gszMsgBuf = "该玻璃cr_proc_id为空！";
				return E_XPMOVEOUT_IS_DM_WIP_WAIT;
			}

			Bis_opeId bis_opeId = new Bis_opeId();
			bis_opeId.setOpe_id(ope_id);
			bis_opeId.setOpe_ver(ope_ver);
			Bis_ope bis_ope = new Bis_ope();
			bis_ope = bisOpeDao.get(bis_opeId);
			if (bis_ope == null) {
				gszMsgBuf = "玻璃所在前一站点没有维护！";
				return E_XPMOVEOUT_IS_DM_WIP_WAIT;
			}

			String nx_path_id_fk = prd_info.getCr_path_id_fk();
			String nx_path_ver_fk = prd_info.getCr_path_ver_fk();

			prd_info.setNx_path_id_fk(nx_path_id_fk);
			prd_info.setNx_path_ver_fk(nx_path_ver_fk);
			prd_info.setNx_ope_id_fk(bis_ope.getId().getOpe_id());

			prd_info.setNx_ope_no_fk(ope_no);

			prd_info.setNx_ope_ver_fk(bis_ope.getId().getOpe_ver());
			prd_info.setNx_proc_id_fk(bis_ope.getProc_id());
			prd_info.setNx_toolg_id_fk(bis_ope.getToolg_id());
			prd_info.setPrd_stat("WAIT");
			prd_info.setEvt_cate(EVT_SKIP_OPE);
			retPrdInfoDao.update(prd_info);

			if (!read_flg) {
				nx_path_id_fk1 = prd_info.getCr_path_id_fk();
				nx_path_ver_fk1 = prd_info.getCr_path_ver_fk();
				nx_ope_no_fk = ope_no;
				nx_ope_id_fk = bis_ope.getId().getOpe_id();
				nx_ope_ver_fk = bis_ope.getId().getOpe_ver();
				nx_proc_id_fk = bis_ope.getProc_id();
				nx_toolG_id_fk = bis_ope.getToolg_id();
			}
		}

		Ret_box ret_box = new Ret_box();
		ret_box = gRet_box;
		ret_box.setEvt_cate(EVT_SKIP_OPE);
		ret_box.setNx_ope_id_fk(nx_ope_id_fk);
		ret_box.setNx_ope_no_fk(nx_ope_no_fk);
		ret_box.setNx_ope_ver_fk(nx_ope_ver_fk);
		ret_box.setNx_path_id_fk(nx_path_id_fk1);
		ret_box.setNx_path_ver_fk(nx_path_ver_fk1);
		ret_box.setNx_proc_id_fk(nx_proc_id_fk);
		ret_box.setNx_toolg_id_fk(nx_toolG_id_fk);
		ret_box.setBox_stat("WAIT");

		retBoxDao.update(ret_box);

		return _NORMAL;
	}

	/**
	 * 镀膜来料检不良增加率、来料检箱良率AMS
	 */
	private void sendICRatioFunc(){
		if("DMIC".equals(gProc_id)){
			AmsdmicI amsdmicI = new AmsdmicI();
			amsdmicI.setTrx_id(T_AMSDMIC);
			amsdmicI.setType_id(TRX_IN);
			amsdmicI.setCus_id(gCus_id);
			amsdmicI.setWo_id(gWo_id);
			amsdmicI.setSo_id(gSo_id);
			amsdmicI.setOpe_dsc(gOpe_dsc);
			amsdmicI.setBox_id(gRet_box.getBox_id());
			amsdmicI.setRetPrdList(gRet_prd_infoList);
			String strInMsg = mqOpe.obj2Str(amsdmicI);
			sendMsgService.sendWithRepaly(strInMsg);//镀膜来料检不良增加率
		}
		//来料检箱良率
		AmsicgoodI amsicgoodI = new AmsicgoodI();
		amsicgoodI.setTrx_id(T_AMSICGOOD);
		amsicgoodI.setType_id(TRX_IN);
		amsicgoodI.setCus_id(gCus_id);
		amsicgoodI.setWo_id(gWo_id);
		amsicgoodI.setSo_id(gSo_id);
		amsicgoodI.setOpe_dsc(gOpe_dsc);
		amsicgoodI.setBox_id(gRet_box.getBox_id());
		amsicgoodI.setRetPrdList(gRet_prd_infoList);
		String strInMsg = mqOpe.obj2Str(amsicgoodI);
		sendMsgService.sendWithRepaly(strInMsg);
	}

	/**
	 * 终检单箱良率AMS
	 */
	private void sendZJRatioFunc(){
		AmszjI amszjI = new AmszjI();
		amszjI.setTrx_id(T_AMSZJ);
		amszjI.setType_id(TRX_IN);
		amszjI.setCus_id(gCus_id);
		amszjI.setWo_id(gWo_id);
		amszjI.setSo_id(gSo_id);
		amszjI.setOpe_dsc(gOpe_dsc);
		amszjI.setBox_id(gRet_box.getBox_id());
		amszjI.setRetPrdList(gRet_prd_infoList);
		String strInMsg = mqOpe.obj2Str(amszjI);
		sendMsgService.sendWithRepaly(strInMsg);
	}

	/**
	 * 一批产品破片告警AMS
	 */
	private void sendLotBrokenCntFunc(){
		AmsbrokenI amsbrokenI = new AmsbrokenI();
		amsbrokenI.setTrx_id(T_AMSLOTBROKEN);
		amsbrokenI.setType_id(TRX_IN);
		amsbrokenI.setCus_id(gCus_id);
		amsbrokenI.setRetPrdList(gRet_prd_infoList);
		String strInMsg = mqOpe.obj2Str(amsbrokenI);
		sendMsgService.sendWithRepaly(strInMsg);
	}

	/**
	 * 生成客户的出货箱号
	 *
	 * @return
	 */
	private long crtShipBoxId() {
		String key_id = "";
		int iSerial = 0;
		Ret_crt_noId id = new Ret_crt_noId();
		String curMdlCate = gMdl_cate.substring(gMdl_cate.length()-1, gMdl_cate.length());
		if((MDL_CATE_JB.equals(curMdlCate) && "JBBZ".equals(gProc_id)) ||
				(MDL_CATE_DM.equals(curMdlCate) && "DMBZ".equals(gProc_id))
				|| "DMBZ".equals(gProc_id)){
//			if("003".equals(gCus_id)){
//				//第1码为月份
//				Calendar rightNow = Calendar.getInstance();
//				key_id = Util.getMonthTyp(rightNow);
//				gShip_box_id = Util.getMonthTyp(rightNow);
//				//第2码为WG薄代码,默认为U
//				gShip_box_id = gShip_box_id + "U";
//				//第3码切割为W，其他均为数字0
//				if("JBZJ".equals(gProc_id) || "DMZJ".equals(gProc_id)){
//					gShip_box_id = gShip_box_id + "0";
//				}else{
//					gShip_box_id = gShip_box_id + "W";
//				}
//				//第4码为默认数字0
//				gShip_box_id = gShip_box_id + "0";
//				//第5、6、7码为流水码
//				id.setKey_id(key_id);
//				id.setType("X");
//				Ret_crt_no ret_crt_no = retCrtNoDao.get(id);
//				if(ret_crt_no==null){
//					ret_crt_no = new Ret_crt_no();
//					ret_crt_no.setId(id);
//				}else{
//					iSerial = Integer.valueOf(ret_crt_no.getSerial_no());
//				}
//				iSerial++;
//				gShip_box_id = gShip_box_id + Util.getSerialNo(iSerial, 3);
//				ret_crt_no.setSerial_no(String.valueOf(iSerial));
//				retCrtNoDao.saveOrUpdate(ret_crt_no);
//			}else if("007".equals(gCus_id)){
			//第1、2码为等级
//				gShip_box_id = gRet_prd_infoList.get(0).getPrd_grade();
//				//第3、4码为固定WA
//				gShip_box_id = gShip_box_id + "WA";
//				//第5、6码为WO中product ID的末两位
//				gShip_box_id = gShip_box_id + gMdl_id.substring(gMdl_id.length()-2, gMdl_id.length());
//				//第7、8码为固定01
//				gShip_box_id = gShip_box_id + "01";
//				//第9、10码为固定00
//				gShip_box_id = gShip_box_id + "00";
//				//第11、12、13、14、15码为西元后两码、月份、日期
//				gShip_box_id = gShip_box_id + Util.getYearMonthDayZero(2,"N");
//				key_id = Util.getYearMonthDayZero(2,"N");
//				//第16~20码为流水码
//				id.setKey_id(key_id);
//				id.setType("X");
//				Ret_crt_no ret_crt_no = retCrtNoDao.get(id);
//				if(ret_crt_no==null){
//					ret_crt_no = new Ret_crt_no();
//					ret_crt_no.setId(id);
//				}else{
//					iSerial = Integer.valueOf(ret_crt_no.getSerial_no());
//				}
//				iSerial++;
//				gShip_box_id = gShip_box_id + Util.comInt2String(iSerial, 5);
//				ret_crt_no.setSerial_no(String.valueOf(iSerial));
//				retCrtNoDao.saveOrUpdate(ret_crt_no);
//			}else
			if("018".equals(gCus_id) ||
					"098".equals(gCus_id) ||
					"088".equals(gCus_id)){
				//第1码为W
				gShip_box_id= "HC";
				//第2码为0或1，箱内全部是非OK品则是1，其他是0
//				for(int i=0; i<gRet_prd_infoList.size(); i++){
//					if(_OK.equals(gRet_prd_infoList.get(i).getPrd_grade())){
//						gShip_box_id = gShip_box_id + "0";
//						break;
//					}
//					if(i == gRet_prd_infoList.size()-1){
//						gShip_box_id = gShip_box_id + "1";
//					}
//				}
				if("OK".equals(inTrx.getPrd_grade())){
					gShip_box_id = gShip_box_id + "0";
				}else{
					gShip_box_id = gShip_box_id + "1";
				}
				//第3、4、5码为年份、月份、日期
				key_id = key_id + HCutil.getYearMonthDayZero(1,"Y");
				gShip_box_id = gShip_box_id + HCutil.getYearMonthDayZero(1,"Y");
				//第6码为单面减薄厚度
				if(gTthickness.equals(gCthickness)){
					gShip_box_id = gShip_box_id + HCutil.getThicknessTyp(gTthickness);
				}else{
					gShip_box_id = gShip_box_id + HCutil.getThicknessTyp(gTthickness + "+" + gCthickness);
				}

				//第7、8码为箱内产品数量
				gShip_box_id = gShip_box_id + HCutil.comInt2String(gRet_prd_infoList.size(), 2);
				//第9、10码为流水号
				id.setKey_id(key_id);
				id.setType("X");
				Ret_crt_no ret_crt_no = retCrtNoDao.get(id);
				if(ret_crt_no==null){
					ret_crt_no = new Ret_crt_no();
					ret_crt_no.setId(id);
				}else{
					iSerial = Integer.valueOf(ret_crt_no.getSerial_no());
				}
				iSerial++;
				//gShip_box_id = gShip_box_id + HCutil.comInt2String(iSerial, 2);
				gShip_box_id = gShip_box_id + HCutil.getSerialNo(iSerial, 2);
				ret_crt_no.setSerial_no(String.valueOf(iSerial));
				retCrtNoDao.saveOrUpdate(ret_crt_no);
			}else if("068".equals(gCus_id))
			{
				List<Ret_prd_in> ret_prd_in_list = new ArrayList<Ret_prd_in>();
				ret_prd_in_list = retPrdInDao
						.find("FROM Ret_prd_in where mtrl_box_id = '" + gRet_box.getBox_id()
								+ "'");
				if(null ==ret_prd_in_list||ret_prd_in_list.size()==0) {  //非原箱 ，shipboxid 参照如下规则生成
					gShip_box_id="H"; // 第一码为 H
					String year_month =HCutil.getYearMonthDayEight(1,"");// 第二码为年份最后一位，第三码为 月份简写
					gShip_box_id=gShip_box_id+year_month;

					id.setKey_id(year_month);
					id.setType("Y");
					Ret_crt_no ret_crt_no = retCrtNoDao.get(id);
					if(ret_crt_no==null){
						ret_crt_no = new Ret_crt_no();
						ret_crt_no.setId(id);
					}else{
						iSerial = Integer.valueOf(ret_crt_no.getSerial_no());
					}
					iSerial++;

					gShip_box_id = gShip_box_id + String.format("%04d", iSerial);//4位流水码
					ret_crt_no.setSerial_no(String.valueOf(iSerial));
					retCrtNoDao.saveOrUpdate(ret_crt_no);
				}else{
					gShip_box_id=gRet_box.getBox_id();  //原箱出货，shipboxid为原箱
				}
			}else if("007".equals(gCus_id)){
				//第1、2码为等级
				gShip_box_id = gRet_prd_infoList.get(0).getPrd_grade();
				//第3、4码为固定WA
				gShip_box_id = gShip_box_id + "BA";
				//第5、6码为WO中product ID的末两位
				gShip_box_id = gShip_box_id + gMdl_id.substring(gMdl_id.length()-2, gMdl_id.length());
				//第7、8码为固定01
				gShip_box_id = gShip_box_id + "01";
				//第9、10码为固定00
				gShip_box_id = gShip_box_id + "00";
				//第11、12、13、14、15码为西元后两码、月份、日期
				gShip_box_id = gShip_box_id + Util.getYearMonthDayZero(2,"N");
				key_id = Util.getYearMonthDayZero(2,"N");
				//第16~20码为流水码
				id.setKey_id(key_id);
				id.setType("X");
				Ret_crt_no ret_crt_no = retCrtNoDao.getWithLock(id,LockMode.UPGRADE);
				if(ret_crt_no==null){
					ret_crt_no = new Ret_crt_no();
					ret_crt_no.setId(id);
				}else{
					iSerial = Integer.valueOf(ret_crt_no.getSerial_no());
				}
				iSerial++;
				gShip_box_id = gShip_box_id + Util.comInt2String(iSerial, 5);
				ret_crt_no.setSerial_no(String.valueOf(iSerial));
				retCrtNoDao.saveOrUpdate(ret_crt_no);
			}
		}
		return _NORMAL;
	}

	/**
	 * 生成客户的出货箱号
	 *
	 * @return
	 */
	private long setPgInfo() {
		Ret_crt_noId id = new Ret_crt_noId();
		Ret_wo ret_wo = new Ret_wo();
		if( "PGGY".equals(gProc_id)){
			ret_wo = retWODao.get(gRet_box.getWo_id_fk());
			if (null == ret_wo) {
				gszMsgBuf = new NotFoundRtnBeen(gRet_prd_infoList.get(0)
						.getWo_id_fk(), Ret_wo.class).toString();
				return E_RET_WO + E_READ_NOT_FOUND;
			}
			if("G5".equals(ret_wo.getPlant_id())){
				for(Ret_prd_info ret_prd :gRet_prd_infoList){
					Ret_pginfo_setId retPginfoSetId = new Ret_pginfo_setId();
					retPginfoSetId.setEvt_cate("LOGOF");
					retPginfoSetId.setPrd_seq_id_fk(ret_prd.getPrd_seq_id());
					gRet_pginfo_set =retPginfoSetDao.get(retPginfoSetId);
					if(null == gRet_pginfo_set){
						Ret_pginfo_set  gRet_pginfo_set_temp = new Ret_pginfo_set();
						gRet_pginfo_set_temp.setId(retPginfoSetId);
						gRet_pginfo_set_temp.setCf_polish_time_spend(       inTrx.getCf_polish_time_spend()   );
						gRet_pginfo_set_temp.setTft_polish_time_spend(      inTrx.getTft_polish_time_spend()  );
						gRet_pginfo_set_temp.setPolish_pressure2(           inTrx.getPolish_pressure2()       );
						gRet_pginfo_set_temp.setUp_disk_frequency(          inTrx.getUp_disk_frequency()      );
						gRet_pginfo_set_temp.setDown_disk_frequency(        inTrx.getDown_disk_frequency()    );
						gRet_pginfo_set_temp.setSwing_range(                inTrx.getSwing_range()            );
						gRet_pginfo_set_temp.setAxle_distance(              inTrx.getAxle_distance()          );

						gRet_pginfo_set_temp.setCr_tool_id_fk(inTrx.getTool_id());
						gRet_pginfo_set_temp.setEvt_timestamp(Util.getCurrentTimestamp());

						gRet_pginfo_set_temp.setEvt_usr(inTrx.getJudge_user());
						gRet_pginfo_set_temp.setFactoryname("WG");
						gRet_pginfo_set_temp.setMtrl_prod_id_fk(ret_prd.getMtrl_prod_id_fk());
						gRet_pginfo_set_temp.setMtrl_box_id_fk(ret_prd.getMtrl_box_id_fk());

						Ret_tm_cus_prd_in mtrlBox = new Ret_tm_cus_prd_in();

						String sql = "SELECT  * FROM Ret_tm_cus_prd_in  WHERE BOXNAME='"+ret_prd.getMtrl_box_id_fk()+"'";
						List<Ret_tm_cus_prd_in> resultList = retTmCusPrdInDao.findBySQL(sql, Ret_tm_cus_prd_in.class);
						if(resultList.size()>0){
							mtrlBox=resultList.get(0);
							gRet_pginfo_set_temp.setCus_info_snd(mtrlBox.getProductrequestname());
						}
						retPginfoSetDao.save(gRet_pginfo_set_temp);

					}else{
						gRet_pginfo_set.setId(retPginfoSetId);
						gRet_pginfo_set.setCf_polish_time_spend(       inTrx.getCf_polish_time_spend()   );
						gRet_pginfo_set.setTft_polish_time_spend(      inTrx.getTft_polish_time_spend()  );
						gRet_pginfo_set.setPolish_pressure2(           inTrx.getPolish_pressure2()       );
						gRet_pginfo_set.setUp_disk_frequency(          inTrx.getUp_disk_frequency()      );
						gRet_pginfo_set.setDown_disk_frequency(        inTrx.getDown_disk_frequency()    );
						gRet_pginfo_set.setSwing_range(                inTrx.getSwing_range()            );
						gRet_pginfo_set.setAxle_distance(              inTrx.getAxle_distance()          );

						gRet_pginfo_set.setCr_tool_id_fk(inTrx.getTool_id());
						gRet_pginfo_set.setEvt_timestamp(Util.getCurrentTimestamp());

						gRet_pginfo_set.setEvt_usr(inTrx.getJudge_user());
						gRet_pginfo_set.setFactoryname("WG");
						gRet_pginfo_set.setMtrl_prod_id_fk(ret_prd.getMtrl_prod_id_fk());

						gRet_pginfo_set.setMtrl_box_id_fk(ret_prd.getMtrl_box_id_fk());
						Ret_tm_cus_prd_in mtrlBox = new Ret_tm_cus_prd_in();

						String sql = "SELECT  * FROM Ret_tm_cus_prd_in  WHERE BOXNAME='"+ret_prd.getMtrl_box_id_fk()+"'";
						List<Ret_tm_cus_prd_in> resultList = retTmCusPrdInDao.findBySQL(sql, Ret_tm_cus_prd_in.class);
						if(resultList.size()>0){
							mtrlBox=resultList.get(0);
							gRet_pginfo_set.setCus_info_snd(mtrlBox.getProductrequestname());
						}
						retPginfoSetDao.update(gRet_pginfo_set);
					}
				}
			}

		}

//			
		return _NORMAL;
	}
}
